设计模式之双向适配器模式
来源:互联网 发布:vb cpu序列号 编辑:程序博客网 时间:2024/05/18 00:42
- 双向适配器模式:将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。
例子:
设计和实现一个双向适配器实例,使得猫Cat可以学狗Dog叫Cry() ,狗可以学猫抓老鼠CatchMouse() 。
类图:
namespace 双向适配器{ class Program { static void Main(string[] args) { DogTarget dog = new ConcreteDog(); CatTarget cat = new ConcreteCat(); dog.Cry(); cat.CatchMouse(); Adapter adapter = new Adapter(dog,cat); adapter.CatchMouse(); adapter.Cry(); Console.ReadKey(); } } public interface DogTarget { void Cry(); } public interface CatTarget { void CatchMouse(); } public class ConcreteCat : CatTarget { public void CatchMouse() { Console.WriteLine("会抓老鼠!"); } } public class ConcreteDog : DogTarget { public void Cry() { Console.WriteLine("汪汪汪.."); }} public class Adapter:CatTarget,DogTarget { private CatTarget cat; private DogTarget dog; public Adapter(DogTarget dogtarget,CatTarget cattarget) { this.dog = dogtarget; this.cat = cattarget; } public void Cry() {cat.CatchMouse(); } public void CatchMouse() { dog.Cry(); } }}
如有错误的地方还请指正!
阅读全文
0 0
- 设计模式之双向适配器模式
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式 之 适配器
- 设计模式 之 适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式 之 适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 找出一个无序数组的中位数
- js中的数据类型及其转换
- 网友亲身经历的数据勒索事件_原来还有这么多种姿势长见识了!
- 常用网址
- C++中的各种预处理的表示
- 设计模式之双向适配器模式
- IntelliJ Idea 常用快捷键列表
- 洛谷 P2085 最小函数值
- 安装MySql-Python遇到的错误及解决方法
- 删除子串
- 非对称加密与数字签名
- WIN7 添加本地打印机为灰色怎么办
- 安装Linux必备知识点
- 海量数据存储--分库分表策略详解