适配器模式

适配器模式

将一个类的接口转换成客户希望的另一个接口,Adapter模式使原本由于接口不兼容而不能工作的那些类可以一起工作。

class Target
{
public:
  virtual void process() = 0;
}

class Adaptee
{
public:
  virtual void foo(int data) = 0;
  virtual int bar() = 0;
}

class Adapter : public Target
{
public:
  virtual void process()
  {
    int data=adaptee_->bar();
    adaptee_->foo(data);
  }
  
private:
  Adaptee adaptee_;
}

int main()
{
  Adaptee* adaptee=new Adaptee();
  Target* target=new Adapter(adaptee);
  target->process();
}

UML类图

posted @ 2021-10-04 17:47  暹罗吹雪  阅读(41)  评论(0编辑  收藏  举报