C++设计模式之适配器模式(Adapter)

2020-01-06 18:25:22王冬梅

适配器模式顾名思义在于接口的转换,最形象的例子就如两口转三口电源适配器,口子的数量可以理解为参数数量,一如我们调用三个参数的接口,而提供的接口只有两个参数,那么久需要适配器类进行接口的扩展改造,这就是适配器模式存在的最主要意义。

作用

将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作,使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。
想使用一个已经存在的类,但如果它的接口,也就是它的方法和你的要求不相同时,就应该考虑用适配器模式。

类视图

类模式适配器

C++,设计模式,适配器模式

对象模式适配器

C++,设计模式,适配器模式

代码实现


//目标接口类,客户需要的接口
class Target
{
public:
  Target(){}
  virtual ~Target(){}
  virtual void Request()//定义新的标准接口
  {
    cout << "Request" << endl;
  }
};

//需要适配的类
class Object
{
public:
  Object(){}
  ~Object(){}
  void SpecificRequest()
  {
    cout << "SpecificRequest" << endl;
  }
};

//类模式,适配器类,通过public继承获得接口继承的效果,通过private继承获得实现继承的效果
class Adapter :public Target, private Object
{
public:
  Adapter(){}
  ~Adapter(){}
  virtual void Request()//实现Target定义的Request接口
  {
    cout << "AdapterRequest" << endl;
    this->SpecificRequest();
    cout << "----------------------------" << endl;
  }
};

//对象模式,适配器类,继承Target类,采用组合的方式实现Object的复用
class Adapter1 :public Target
{
public:
  Adapter1(Object* adaptee) :_adaptee(adaptee)
  {}
  Adapter1() :_adaptee(new Object)
  {}
  ~Adapter1(){}
  virtual void Request()//实现Target定义的Request接口
  {
    cout << "Adapter1Request" << endl;
    _adaptee->SpecificRequest();
    cout << "----------------------------" << endl;
  }
private:
  Object* _adaptee;
};

//client调用
int main()
{
  //类模式Adapter
  Target* pTarget = new Adapter();
  pTarget->Request();

  //对象模式Adapter1
  Adaptee* ade = new Adaptee();
  Target* pTarget1= new Adapter1(ade);
  pTarget1->Request();

  //对象模式Adapter2
  Target* pTarget2 = new Adapter1();
  pTarget2->Request();

  return 0;
}