《大话设计模式》学习笔记18:桥接模式
手机品牌及手机软件示例:
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(); } }
“将抽象部分与它的现实部分分离”即实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来让它们独立变化,减少它们之间的耦合。
posted on 2015-05-23 12:51 Walden1024 阅读(224) 评论(0) 编辑 收藏 举报