首页 > 其他 > 详细

《大话设计模式》学习笔记18:桥接模式

时间:2015-05-23 14:05:30      阅读:247      评论:0      收藏:0      [点我收藏+]

  技术分享

  技术分享

手机品牌及手机软件示例:

  技术分享

1.Implementor:

    public abstract class HandsetSoft
    {
        public abstract void Run();
    }

2.ConcreteImplementor(以游戏类为例):

    public class HandsetGame:HandsetSoft
    {
        public override void Run()
        {
            Console.WriteLine("运行手机游戏");
        }
    }

3.Abstraction:

    public abstract class HandsetBrand
    {
        protected HandsetSoft handsetSoft;
        public void SetHandsetSoft(HandsetSoft handsetSoft)
        {
            this.handsetSoft = handsetSoft;
        }
        public abstract void Run();
    }

4.RefinedAbstraction(以手机品牌N为例):

    public class HandsetBrandN:HandsetBrand
    {
        public override void Run()
        {
            handsetSoft.Run();
        }
    }

5.客户端代码:

    class Program
    {
        static void Main(string[] args)
        {
            HandsetBrand handsetBrand;

            handsetBrand = new HandsetBrandN();
            handsetBrand.SetHandsetSoft(new HandsetGame());
            handsetBrand.Run();
            handsetBrand.SetHandsetSoft(new HandsetAddressList());
            handsetBrand.Run();

            handsetBrand = new HandsetBrandM();
            handsetBrand.SetHandsetSoft(new HandsetGame());
            handsetBrand.Run();
            handsetBrand.SetHandsetSoft(new HandsetAddressList());
            handsetBrand.Run();
        }
    }

 

  “将抽象部分与它的现实部分分离”即实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来让它们独立变化,减少它们之间的耦合。

 

《大话设计模式》学习笔记18:桥接模式

原文:http://www.cnblogs.com/walden1024/p/4523984.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!