Adapter设计模式-HeadFirst学习
来源:互联网 发布:淘宝店铺装修模块图片 编辑:程序博客网 时间:2024/06/13 11:05
1、适配器模式定义
将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间
2、要点
与外观模式一起总结
3、例子代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace AdapterPattern{ /// <summary> /// 具备飞行和呱呱叫能力的鸭子接口 /// </summary> public interface IDuck { void quack(); void fly(); } /// <summary> /// 绿头鸭是实现了鸭子接口的鸭子 /// </summary> public class GreenHeadDuck : IDuck { #region Duck 成员 public void quack() { Console.WriteLine("GreenHeadDuck_quack") ; } public void fly() { Console.WriteLine("GreenHeadDuck_fly"); } #endregion } /// <summary> /// 新的接头顽禽 /// </summary> public interface ITurkey { void gobble(); void fly(); } /// <summary> /// 野火鸡 /// </summary> public class WildTurkey : ITurkey { #region Turkey 成员 public void gobble() { Console.WriteLine("WildTurkey_gobble"); } public void fly() { Console.WriteLine("WildTurkey_fly"); } #endregion } /// <summary> /// 包装成鸭子模样的火鸡 /// </summary> public class TurkeyAdapter : IDuck { private ITurkey m_pTurkey; public TurkeyAdapter(ITurkey pTurkey) { m_pTurkey = pTurkey; } #region IDuck 成员 public void quack() { m_pTurkey.gobble(); } public void fly() { for (int i = 0; i < 5; i ++ ) m_pTurkey.fly(); } #endregion }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace AdapterPattern{ class Program { static void Main(string[] args) { //现在没有现成的鸭子,只好拿一只野鸡来滥竽充数了 IDuck pDuck = new TurkeyAdapter(new WildTurkey()); pDuck.quack(); pDuck.fly(); } }}
4、主要类图
0 0
- Adapter设计模式-HeadFirst学习
- 设计模式(Headfirst)学习
- Command设计模式-HeadFirst学习
- MVC设计模式--HeadFirst学习
- HeadFirst设计模式学习笔记
- HeadFirst设计模式学习之组合模式
- 《HeadFirst 设计模式》学习笔记(一)
- HeadFirst设计模式
- HeadFirst -------- 设计模式要点
- Headfirst设计模式 整理
- HeadFirst 设计模式 总
- HeadFirst设计模式
- HeadFirst 设计模式 笔记
- Headfirst-设计模式
- HeadFirst设计模式 读书笔记
- HeadFirst设计模式 读书笔记
- 读headFirst设计模式
- 读书笔记:《HeadFirst设计模式》
- position:absolute;
- Linux五个查找命令
- 软中断网卡处理&Linux高性能外部设备处理机制
- Codeforces_231A
- IGMP
- Adapter设计模式-HeadFirst学习
- 10022--InvocationHandler中invoke()方法的调用问题
- 入手cocos2dx3.9笔记 3 tolua++解析
- Android 相关知识链接
- JavaWeb学习总结(二)——Tomcat服务器学习和使用(一)
- 学习C语言之函数导入二维数组及随机数函数的编写
- 读写锁
- ie打印
- linux socket 编程(C语言)--转载