设计模式-适配器模式(adapter pattern)
来源:互联网 发布:淘宝lolita鞋子 编辑:程序博客网 时间:2024/05/01 00:33
名称: 适配器模式
说说:还记得 xxx 类下面的 xxxWrapper 类吗?
动机:
适用性:
参与者:
结果:将一个类的接口,转换成客户期望的另一个接口
类图(类适配):
类图(对象适配):
说明:感觉对象适配用例要多一点。
demo c#:
namespace adapter { class Program { static void Main(string[] args) { Console.WriteLine("类适配:"); target _t = new adapter_class(); _t.speakChinese(); _t.speakJapanese(); _t.speakEnglish(); Console.WriteLine("对象适配:"); adaptee _a = new adaptee(); target _t2 = new adapter_object(_a); _t2.speakChinese(); _t2.speakJapanese(); _t2.speakEnglish(); Console.Read(); } } // main code interface target { // 期望功能 void speakChinese(); void speakJapanese(); void speakEnglish(); } class adaptee { // 源角色 public void showChinese() { Console.WriteLine("嗯,你说的对!"); } public void showJanpanese() { Console.WriteLine("雅蠛蝶~~"); } } // 类适配器(继承实现),无需重新实现adaptee,高效 class adapter_class : adaptee, target { // 适配器 public void speakChinese() { this.showChinese(); } public void speakJapanese() { this.showJanpanese(); } public void speakEnglish() { Console.WriteLine("hello world!"); } } // 对象适配器(组合实现),更有弹性,没有父类方法被覆盖的隐患 class adapter_object : target { adaptee _adaptee; public adapter_object(adaptee a) { this._adaptee = a; } public void speakChinese() { this._adaptee.showChinese(); } public void speakJapanese() { this._adaptee.showJanpanese(); } public void speakEnglish() { Console.WriteLine("hello world!"); } }}
0 0
- 设计模式-适配器模式(adapter pattern)
- 适配器设计模式(Adapter Pattern)
- 设计模式 - Adapter Pattern(适配器模式)
- 设计模式 - Adapter Pattern(适配器模式)
- 设计模式-适配器模式(Adapter Pattern)
- 设计模式-适配器模式(Adapter Pattern)
- 设计模式之适配器模式(Adapter Pattern)
- c++设计模式:适配器模式(Adapter Pattern)
- 设计模式 - 适配器模式(adapter pattern) 详解
- 设计模式-适配器模式(Adapter Pattern)
- 设计模式--适配器模式【 Adapter Pattern】
- 设计模式之适配器模式--- Pattern Adapter
- Java设计模式--适配器模式【Adapter Pattern】
- 设计模式【适配器模式Adapter Pattern】
- 设计模式--适配器模式(Adapter Pattern)
- 【设计模式】适配器模式(Adapter Pattern)
- 适配器模式(Adapter Pattern)
- 适配器模式(Adapter Pattern)
- hdu 1997 汉诺塔VII(递归)
- Jeditable - jQuery就地编辑插件的使用
- 两道练习题
- JS 调用函数时有括号跟没括号的区别
- Codeforces Round #297 (Div. 2) -- D. Arthur and Walls (判断矩形)
- 设计模式-适配器模式(adapter pattern)
- iOS入门
- html表格标签
- 红桃3
- (window,parent,opener,top).location.reload方法汇总
- how to implement WaitForMultipleObjects in linux
- InstallShield Limit Edition打包项目为安装文件(VS2012)
- subview在SuperView的bounds之外接收触摸信息。以及subview的响应
- Linux 环境下使用g++编译C++