『阅读笔记』设计模式3
cheny658 Lv3

适配器模式

假设有两个类A, B想使用同一个接口,可以继承其中一个类(假如说是A)并叫它C,那么A的实例就随便调了,主要是B。在C的override function里按B的调用方式实现就好了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class A {
public:
virtual process() const {
std::cout << "Process A here" << std::endl;
}
};

class B {
public:
virtual process_ex() const {
std::cout << "Process_ex B here" << std::endl;
}
};

class C : public A {
public:
C(B* b) : b_(b){};

virtual process() const override {
b_->process_ex();
}
private:
B* b_;
};

void call_process(A* ptr) {
ptr->process();
}

int main() {
A a;
B b;
C c(&b);
call_process(&a);
call_process(&c);
}

桥接模式

如果对于抽象层的行为有新的需求,那么使用该模式override抽象类的接口。感觉是比较泛用的方法,讲道理抽象类完全可以由一个只提供接口类继承出来,这样所有抽象类派生类都是平级的,方便理解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* The Implementation defines the interface for all implementation classes. It
* doesn't have to match the Abstraction's interface. In fact, the two
* interfaces can be entirely different. Typically the Implementation interface
* provides only primitive operations, while the Abstraction defines higher-
* level operations based on those primitives.
*/

class Implementation {
public:
virtual ~Implementation() {}
virtual std::string OperationImplementation() const = 0;
};

/**
* Each Concrete Implementation corresponds to a specific platform and
* implements the Implementation interface using that platform's API.
*/
class ConcreteImplementationA : public Implementation {
public:
std::string OperationImplementation() const override {
return "ConcreteImplementationA: Here's the result on the platform A.\n";
}
};
class ConcreteImplementationB : public Implementation {
public:
std::string OperationImplementation() const override {
return "ConcreteImplementationB: Here's the result on the platform B.\n";
}
};

/**
* The Abstraction defines the interface for the "control" part of the two class
* hierarchies. It maintains a reference to an object of the Implementation
* hierarchy and delegates all of the real work to this object.
*/

// 本人观点,可以提供一个IAbstraction如下
// class IAbstraction {
// protected:
// Implementation* implementation_;

// public:
// IAbstraction(Implementation* implementation) : implementation_(implementation) {
// }

// virtual ~IAbstraction() {
// }

// virtual std::string Operation() const = 0;
// }
// 然后让后面的Abstraction和Abstraction和ExtendedAbstraction继承这个类,这样更好管理两个派生类

class Abstraction {
/**
* @var Implementation
*/
protected:
Implementation* implementation_;

public:
Abstraction(Implementation* implementation) : implementation_(implementation) {
}

virtual ~Abstraction() {
}

virtual std::string Operation() const {
return "Abstraction: Base operation with:\n" +
this->implementation_->OperationImplementation();
}
};
/**
* You can extend the Abstraction without changing the Implementation classes.
*/
class ExtendedAbstraction : public Abstraction {
public:
ExtendedAbstraction(Implementation* implementation) : Abstraction(implementation) {
}
std::string Operation() const override {
return "ExtendedAbstraction: Extended operation with:\n" +
this->implementation_->OperationImplementation();
}
};

/**
* Except for the initialization phase, where an Abstraction object gets linked
* with a specific Implementation object, the client code should only depend on
* the Abstraction class. This way the client code can support any abstraction-
* implementation combination.
*/
void ClientCode(const Abstraction& abstraction) {
// ...
std::cout << abstraction.Operation();
// ...
}
/**
* The client code should be able to work with any pre-configured abstraction-
* implementation combination.
*/

int main() {
Implementation* implementation = new ConcreteImplementationA;
Abstraction* abstraction = new Abstraction(implementation);
ClientCode(*abstraction);
std::cout << std::endl;
delete implementation;
delete abstraction;

implementation = new ConcreteImplementationB;
abstraction = new ExtendedAbstraction(implementation);
ClientCode(*abstraction);

delete implementation;
delete abstraction;

return 0;
}