码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-23 14:08:35      阅读:125      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!