设计模式:结构型模式:适配器(adapter)
来源:互联网 发布:eview触摸屏软件 编辑:程序博客网 时间:2024/06/05 07:49
http://www.codeproject.com/Tips/595716/Adapter-Design-Pattern-in-Cplusplus
Using the Code
An adapter pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
It comprises three components:
Target
: This is the interface with which the client interacts.Adaptee
: This is the interface the client wants to interact with, but can’t interact without the help of theAdapter
.Adapter
: This is derived from Target and contains the object ofAdaptee
.
When I shifted from India to London, I took along many of my electrical appliances with me. But there was one problem using them, the pin shape. In India, all the appliances have round pins whereas in London they are flat pinned. Now how do we overcome this problem as I was not ready to buy new plugs. So the adapter came to my rescue (and saved lots of pounds!!!)
I got hold of an adapter plug which has round pins as input and the other end with flat pins (India to UK adapters). This is how I used them with the adapter pattern.
Class description:
<AbstractPlug>
: Abstract Target class<Plug>
: Concrete Target class<AbstractSwitchBoard>
: Abstract Adaptee class<SwitchBoard>
: Concrete Adaptee class<Adapter>
: Adapter class, our saviour
// Abstract Targetclass AbstractPlug {public: void virtual RoundPin(){} void virtual PinCount(){}};// Concrete Targetclass Plug : public AbstractPlug {public: void RoundPin() { cout << " I am Round Pin" << endl; } void PinCount() { cout << " I have two pins" << endl; }};// Abstract Adapteeclass AbstractSwitchBoard {public: void virtual FlatPin() {} void virtual PinCount() {}};// Concrete Adapteeclass SwitchBoard : public AbstractSwitchBoard {public: void FlatPin() { cout << " Flat Pin" << endl; } void PinCount() { cout << " I have three pins" << endl; }};// Adapterclass Adapter : public AbstractPlug {public: AbstractSwitchBoard *T; Adapter(AbstractSwitchBoard *TT) { T = TT; } void RoundPin() { T->FlatPin(); } void PinCount() { T->PinCount(); }};// Client codevoid _tmain(int argc, _TCHAR* argv[]){ SwitchBoard *mySwitchBoard = new SwitchBoard; // Adaptee // Target = Adapter(Adaptee) AbstractPlug *adapter = new Adapter(mySwitchBoard); adapter->RoundPin(); adapter->PinCount();}
- 结构型设计模式---适配器模式(adapter)
- 设计模式(五)适配器模式Adapter(结构型)
- 设计模式(五)适配器模式Adapter(结构型)
- 设计模式(五)适配器模式Adapter(结构型)
- 设计模式(五)适配器模式Adapter(结构型)
- 设计模式(五)适配器模式Adapter(结构型)
- 设计模式(六)适配器模式(Adapter)-结构型
- 设计模式(五)适配器模式Adapter(结构型)
- 设计模式(3)-结构型-适配器模式(Adapter)
- 设计模式--适配器模式Adapter(结构型)
- 设计模式:结构型模式:适配器(adapter)
- 设计模式-结构型- 适配器模式(Adapter)
- [设计模式-结构型]适配器(Adapter)
- Adapter适配器模式(结构型模式)
- Adapter模式(适配器模式 结构型)
- 结构型设计模式---Adapter模式(适配器模式)
- 设计模式 一 适配器模式 Adapter(结构型模式)
- 设计模式(6) 结构型模式和适配器模式(ADAPTER)
- POJ1128 Frame Stacking(拓扑排序)经典
- 异常捕获不抛出,Spring事务无法回滚
- 一批大小不同的图片 经过处理变成全部统一指定大小的图片
- android thread Interrupte使用方法
- “爱国者”连锁网络管理系统
- 设计模式:结构型模式:适配器(adapter)
- C++ 复制构造函数和赋值运算符重载函数
- Android编码规范风格指导(翻译)
- 计算二叉树宽度——层次遍历
- 4/28纪实——HCBA项目
- 在facebook上做app, 需要提交privacy policy url的解决办法
- android中EditText插入用户名(块)效果(EditText插入图片)
- [漏洞分析] BEdita CMS 3.5.0中存储型XSS与CSRF漏洞分析
- 2754: C++习题-快速排序