Adapter模式

作者: 老练子丶2017 | 来源:发表于2017-11-22 15:51 被阅读0次

分为类模式和对象两种

类模式public继承接口,private继承实现

Adapter.h

#ifndef _ADAPTER_H

#define _ADAPTER_H

#include <iostream>

using namespace std;

class Target

{

public:

Target() {}

virtual ~Target() {}

virtual void Request() {

cout << "Target:Request" << endl;

}

};

class Adaptee

{

public:

Adaptee() {}

~Adaptee() {}

void SpecificRequest() {

cout << "Adaptee:SpecificRequest" << endl;

}

};

class Adapter : public Target,private Adaptee

{

public:

Adapter() {}

~Adapter() {}

void Request() {

SpecificRequest();

}

};

#endif // _ADAPTER_H

Adapter.cpp

#include "Adapter.h"

int main()

{

Adapter adapter;

adapter.Request();

return 0;

}

编译:make Adapter

对象模式使用委托(组合)来实现,这种写法应该见过挺多了

Adapter.h

#ifndef _ADAPTER_H

#define _ADAPTER_H

#include <iostream>

using namespace std;

class Target

{

public:

Target() {}

virtual ~Target() {}

virtual void Request() {

cout << "Target:Request" << endl;

}

};

class Adaptee

{

public:

Adaptee() {}

~Adaptee() {}

void SpecificRequest() {

cout << "Adaptee:SpecificRequest" << endl;

}

};

class Adapter : public Target

{

public:

Adapter(Adaptee* ade) {

_ade = ade;

}

~Adapter() {}

void Request() {

_ade->SpecificRequest();

}

private:

Adaptee* _ade;

};

#endif // _ADAPTER_H

Adapter.cpp:

#include "Adapter.h"

int main()

{

Adaptee* ade = new Adaptee;

Target* adt = new Adapter(ade);

adt->Request();

return 0;

}

编译:make Adapter

相关文章

  • 11.3设计模式-适配器-详解

    设计模式-适配器adapter模式 adapter模式详解 adapter模式在android中的实际运用1.li...

  • 浅谈设计模式之适配器模式

    适配器模式(Adapter Pattern) 概述: 在设计模式中,适配器模式(adapter pattern)有...

  • Adapter模式

    java设计模式 类适配器模式(使用继承) 接口A中没有我们想要的方法 ,接口B中有合适的方法,不能改变访问接口A...

  • Adapter模式

    一、概述 将类的接口转换为客户端期望的另一个接口。 二、使用 2.1UML结构图

  • Adapter模式

    分为类模式和对象两种 类模式public继承接口,private继承实现 Adapter.h #ifndef _A...

  • 图解设计模式Adapter模式

    Adapter(适配器模式) 适配器模式用于填补现有程序和所需程序之间的差异 Adapter模式有以下两种 类适配...

  • 浅谈GoF23设计模式-“Adapter”模式

    “Adapter”模式为结构型设计模式,C#当中主要使用对象适配器。“Adapter”模式定义:将一个类的接口转换...

  • 适配器模式

    适配器模式简介 适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口。Adapter 模式使...

  • 面向对象编程设计模式------适配器模式

    适配器模式   适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原...

  • 适配器模式(Adapter模式)详解

    模式的定义 适配器模式(Adapter)将一个类的接口转换成客户希望的另外一个接口。Adapter 模式使得原本由...

网友评论

    本文标题:Adapter模式

    本文链接:https://www.haomeiwen.com/subject/cxxovxtx.html