c++適配器模式(Adapter Pattern)

1.類適配器模式


Adaptee类没有Request方法,而客户期待这个方法。为了使客户能够使用Adaptee类,提供一个中间环节,即类Adapter类,Adapter类实现了Target接口,并继承自AdapteeAdapter类的Request方法重新封装了AdapteeSpecificRequest方法,实现了适配的目的。

因为AdapterAdaptee是继承的关系,所以这决定了这个适配器模式是类的。

该适配器模式所涉及的角色包括:

扫描二维码关注公众号,回复: 2810200 查看本文章

目标(Target)角色:这是客户所期待的接口。因为C#不支持多继承,所以Target必须是接口,不可以是类。
源(Adaptee)角色:需要适配的类。
适配器(Adapter)角色:把源接口转换成目标接口。这一角色必须是类


#include<iostream>  
using namespace std;  
  
// "ITarget"  
class Target  
{  
public:  
    // Methods  
    virtual void Request(){};  
};  
  
// "Adaptee"  
class Adaptee  
{  
public:  
    // Methods  
    void SpecificRequest()  
    {  
        cout<<"Called SpecificRequest()"<<endl;  
    }  
};  
  
// "Adapter"  
class Adapter : public Adaptee, public Target  
{  
public:  
    // Implements ITarget interface  
    void Request()  
    {  
        // Possibly do some data manipulation  
        // and then call SpecificRequest    
        this->SpecificRequest();  
    }  
};  
  
  
int main()  
{  
    // Create adapter and place a request  
    Target *t = new Adapter();  
    t->Request();  
  
    return 0;  
}  

2.對象適配器模式


客户端需要调用Request方法,而Adaptee没有该方法,为了使客户端能够使用Adaptee类,需要提供一个包装(Wrapper)类Adapter。这个包装类包装了一个Adaptee的实例,从而将客户端与Adaptee衔接起来。由于AdapterAdaptee是委派关系,这决定了这个适配器模式是对象的。

该适配器模式所涉及的角色包括:

目标(Target)角色:这是客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口。
源(Adaptee)角色:需要适配的类。
适配器(Adapter)角色:通过在内部包装(Wrap)一个Adaptee对象,把源接口转换成目标接口。


#include<iostream>  
using namespace std;  
  
// "ITarget"  
class Target  
{  
public:  
    // Methods  
    virtual void Request(){};  
};  
  
// "Adaptee"  
class Adaptee  
{  
public:  
    // Methods  
    void SpecificRequest()  
    {  
        cout<<"Called SpecificRequest()"<<endl;  
    }  
};  
  
// "Adapter"  
class Adapter : public Target  
{  
private:  
    Adaptee *adaptee;  
  
public:  
    Adapter()  
    {  
        adaptee = new Adaptee();  
    }  
  
    // Implements ITarget interface  
    void Request()  
    {  
        // Possibly do some data manipulation  
        // and then call SpecificRequest    
        adaptee->SpecificRequest();  
    }  
};  
  
  
int main()  
{  
    // Create adapter and place a request  
    Target *t = new Adapter();  
    t->Request();  
  
    return 0;  
}  

3.缺省模式

缺省适配器模式是一种特殊的适配器模式,但这个适配器是由一个抽象类实现的,并且在抽象类中要实现目标接口中所规定的所有方法,但很多方法的实现都是平庸的实现,也就是说,这些方法都是空方法。而具体的子类都要继承此抽象类。

#include<iostream>  
using namespace std;  
  
  
class Target {   
public:  
    virtual void f1(){};   
    virtual void f2(){};   
    virtual void f3(){};     
};  
  
class DefaultAdapter : public Target   
{   
public:  
    void f1() {   
    }   
  
    void f2() {   
    }   
  
    void f3() {   
    }   
};  
  
class MyInteresting :public DefaultAdapter  
{   
public:  
     void f3(){         
        cout<<"呵呵,我就对f3()方法感兴趣,别的不管了!"<<endl;  
    }   
};  
  
int main()  
{  
    // Create adapter and place a request  
    Target *t = new MyInteresting();  
    t->f3();  
  
    return 0;  
}  

实现要点:

1.Adapter模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况,在遗留代码复用、类库迁移等方面非常有用

2Adapter模式有对象适配器和类适配器两种形式的实现结构,但是类适配器采用多继承的实现方式,带来了不良的高耦合,所以一般不推荐使用。对象适配器采用对象组合的方式,更符合松耦合精神。

3Adapter模式的实现可以非常的灵活,不必拘泥于GOF23中定义的两种结构。例如,完全可以将Adapter模式中的现存对象作为新的接口方法参数,来达到适配的目的。

4Adapter模式本身要求我们尽可能地使用面向接口的编程风格,这样才能在后期很方便的适配使用场景:

在以下各种情况下使用适配器模式:

1.系统需要使用现有的类,而此类的接口不符合系统的需要。

2.想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作。这些源类不一定有很复杂的接口。

3.(对对象适配器而言)在设计里,需要改变多个已有子类的接口,如果使用类的适配器模式,就要针对每一个子类做一个适配器,而这不太实际。


猜你喜欢

转载自blog.csdn.net/qq_23516957/article/details/80776739