-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecorator.cpp
More file actions
81 lines (64 loc) · 2.01 KB
/
Copy pathDecorator.cpp
File metadata and controls
81 lines (64 loc) · 2.01 KB
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
#include <iostream>
using namespace std;
/*******************************************************/
// Decoraterパターン
// あるクラスに処理を追加したいが、結合が強く、分割しづらい場合に
// 上からラッパークラスを作って、拡張機能を追加できるようにしたパターン
/*******************************************************/
/*******************************************************/
//------------------------------------------------------
// インターフェース
//------------------------------------------------------
class IComponent
{
public:
virtual void Something() = 0;
};
/*******************************************************/
//------------------------------------------------------
// 普通のコンポーネント
// 既存処理が入っているクラス
//------------------------------------------------------
class SimpleComponent : public IComponent
{
public:
void Something() override;
};
void SimpleComponent::Something()
{
cout << "SimpleComponent::Something" << endl;
}
/*******************************************************/
//------------------------------------------------------
// 拡張したコンポーネント
// この中に拡張処理を入れる。
//------------------------------------------------------
class DecoratorComponent : public IComponent
{
public:
DecoratorComponent(IComponent* decoratedComponent);
void Something() override;
void SomethingElse();
private:
IComponent* decoratedComponent;
};
DecoratorComponent::DecoratorComponent(IComponent* decoratedComponent)
{
this->decoratedComponent = decoratedComponent;
}
void DecoratorComponent::Something()
{
SomethingElse();
decoratedComponent->Something();
}
void DecoratorComponent:: SomethingElse()
{
cout << "DecoratorComponent:: SomethingElse" << endl;
}
/*******************************************************/
int main()
{
IComponent* a = new DecoratorComponent(new SimpleComponent());
a->Something();
return 0;
}