Adapter(适配器模式)属于结构型模式,结构性模式关注的是如何组合类与对象,以获得更大的结构,我们平常工作大部分时间都在与这种设计模式打交道。
- 意图:将一个类的接口转换成客户希望的另一个接口。Adapter 模式使得原本由于接口不兼容而不能在一起工作的那些类可以一起工作。
适配器模式遵照了以下几个设计原则:
- 依赖倒转原则
- 迪米特法则
适配器模式分为:类适配器 和 对象适配
- 构造Adapter类继承Target类,并实现Adaptee接口(适配器模式的类版本)
- 将一个Adaptee实例作为Adapter的组成部分(适配器模式的对象版本)
#include对象适配:using namespace std; enum sqlType { mysqlType = 1, sqlite3Type, postgersqlType }; enum sqlType type; class mysql { public: void select() { cout<<"mysql select"< orm_select(); init_sqltype(sqlite3Type); o->orm_select(); init_sqltype(postgersqlType); o->orm_select(); delete o; return 0; }



