-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
168 lines (139 loc) · 3.25 KB
/
main.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// main.cpp
// Design Pattern
//
// Created by double Z on 2019/10/11.
// Copyright © 2019 double Z. All rights reserved.
//
#include <iostream>
#include "TemplateMethod.h"
#include "Strategy.h"
#include "Observer.h"
#include "Iterator.h"
#include "Singleton.h"
#include "myString.h"
#include "ReferenceCounting.h"
#include "CopyOnWrite.h"
#include "Shareable.h"
#include "FactoryMethod.h"
#include "Property.h"
#include "Composite.h"
#include "BeforeCommand.h"
#include "Command.h"
#include "Memento.h"
#include "Singleton.h"
using namespace std;
void Test_TemplateMethod()
{
CMyDoc myDoc;
myDoc.OnFileOpen();
}
void Test_Strategy()
{
int op;
StrategyTest stest;
while(true){
/* 有新的策略,在这里修改一下UI */
cout << "你想使用那种解决方案1~3 [0 to quit] " << endl;
cin >> op;
if(!op){break;}
stest.setStrategy(op, 999);
stest.run();
}
}
void Test_Observer()
{
Subject subj;
DivObserver divobs1(4); subj.addObserver(&divobs1);
DivObserver divobs2(3); subj.addObserver(&divobs2);
ModObserver modobs3(3); subj.addObserver(&modobs3);
subj.updateValue(14);
}
void Test_Iterator()
{
Iterator();
}
void Test_Singleton()
{
Singleton::Instance();
MayersSingleton::Instance();
}
void Test_ReferenceCounting()
{
myString str = "hello"; //myString(const char* cstr);
myString str1; //myString() {}
str1 = str; //myString& operator=(const myString& rhs);
myString str2(str); //myString(const myString& rhs);
}
void Test_CopyOnWrite()
{
const myString cstr = "hello";
cout << cstr[2]; //调用@1
myString str = "world";
myString str1(str);
cout << str[2]; //调用@2 read
myString str2;
str2 = str;
str[1] = 'O'; //调用@2 write
}
void Test_Shareable()
{
myString s1 = "hello";
char *p = &s1[1]; //使用了non-const operator[],悲观的认为是外界拥有handle了 => 将shareable设为false
myString s2 = s1; //这里s1的shareable已经被设为false,所以此时s2的value要new一个新的
*p = 'x';
}
void Test_FactoryMethod()
{
myApp *myapp = new myApp();
myapp->NewDocument("doc1");
myapp->NewDocument("doc2");
myapp->OutputDocument();
}
void Test_Property()
{
testProperty();
}
void Test_Composite()
{
Composite containers[4];
for(int i=0;i<4;++i){
for(int j=0;j<3;++j){
containers[i].add(new Leaf(i*3 + j));
}
}
for(int i=1;i<4;++i){
containers[0].add(&(containers[i]));
}
for(int i=0;i<4;++i){
containers[i].traverse();
cout << endl;
}
}
void Test_Command()
{
cout << "=== Before Command ===" << endl;
beforeCommand();
cout << endl << "=== After Command ===" << endl;
afterCommand();
}
void Test_Memento()
{
testMemento();
}
int main(int argc, const char * argv[]) {
Test_TemplateMethod();
Test_Strategy();
Test_Observer();
Test_Iterator();
Test_Singleton();
Test_ReferenceCounting();
Test_CopyOnWrite();
Test_Shareable();
Test_FactoryMethod();
Test_Property();
Test_Composite();
Test_Command();
Test_Memento();
return 0;
}