Skip to content

Commit dfbc0fc

Browse files
committed
add mediator pattern
1 parent b9aeeac commit dfbc0fc

File tree

14 files changed

+500
-9
lines changed

14 files changed

+500
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
[迭代器模式 Iterator](doc/18-迭代器.md)
4848

49-
中介者模式 Mediator
49+
[中介者模式 Mediator](doc/19-中介者.md)
5050

5151
备忘录模式 Memento
5252

code/17_iterator/client.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "iterator_types.h"
44

5-
void mysleep(int count);
5+
void mysleep(long count);
66

77
int main(){
88
std::cout<<"Iterator pattern case:"<<std::endl;
@@ -13,19 +13,25 @@ int main(){
1313
list.push_back(new Post("Post 4"));
1414
list.push_back(new Post("Post 5"));
1515

16+
const int POST_SIZE = list.size();
1617
PostBox* box = new PostBox(list);
1718
Iterator* iter = box->createIterator();
1819

19-
while(iter->hasNext()){
20-
box->switchAndShow(iter->next());
21-
mysleep(10000);
20+
while(iter->hasNext()){
21+
int postIndex = iter->next();
22+
box->switchAndShow(postIndex);
23+
if(postIndex == (POST_SIZE-1)){
24+
std::cout<< " --- " <<std::endl;
25+
}
26+
mysleep(50000000);
2227
}
2328

2429
return 0;
2530
}
2631

27-
void mysleep(int count){
28-
for(int i=0;i<count;i++){
29-
32+
void mysleep(long count){
33+
34+
for(long i=0;i<count;i++){
35+
3036
}
3137
}

code/17_iterator/result.png

12.8 KB
Loading

code/18_mediator/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
PROJECT(mediator_test)
3+
4+
include(CheckCXXCompilerFlag)
5+
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
6+
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
7+
8+
# 使用变量设置编译标志
9+
if(COMPILER_SUPPORTS_CXX11)
10+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
11+
elseif(COMPILER_SUPPORTS_CXX0X)
12+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
13+
else()
14+
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
15+
endif()
16+
17+
18+
SET(SRC_LIST client.cpp)
19+
SET(PATTERN_SRC mediator_types.cpp)
20+
21+
include_directories(./)
22+
23+
ADD_EXECUTABLE(mediator_test ${SRC_LIST} ${PATTERN_SRC})

code/18_mediator/client.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "mediator_types.h"
2+
3+
int main(){
4+
Mediator* callCeneter = new CallCenter();
5+
6+
Colleague* userA = new UserA("1001",callCeneter);
7+
Colleague* userB = new UserB("1002",callCeneter);
8+
Colleague* userC = new UserC("1003",callCeneter);
9+
10+
callCeneter->registerColleague(userA);
11+
callCeneter->registerColleague(userB);
12+
callCeneter->registerColleague(userC);
13+
14+
userA->dial("1002");
15+
userA->dial("1003");
16+
17+
userC->dial("1001");
18+
userC->dial("1002");
19+
userC->dial("1004");
20+
21+
return 0;
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "mediator_types.h"
2+
3+
void Colleague::dial(string number){
4+
if(!callCeneter){
5+
cout<<"Please set call center"<<endl;
6+
return;
7+
}
8+
Colleague* dest = callCeneter->pick(number);
9+
if(dest){
10+
dest->reply();
11+
}else{
12+
cout<<"Not found dest:"<<number<<endl;
13+
}
14+
}
15+
16+
string Colleague::getNumber(){
17+
return this->myNumber;
18+
}
19+
20+
void CallCenter::registerColleague(Colleague* colleague){
21+
list.push_back(colleague);
22+
}
23+
24+
Colleague* CallCenter::pick(string number){
25+
Colleague* ret = NULL;
26+
auto it = list.begin();
27+
while(it!=list.end()){
28+
if(number == (*it)->getNumber()){
29+
ret = (*it);
30+
break;
31+
}
32+
it++;
33+
}
34+
return ret;
35+
}
36+
37+
void UserA::reply(){
38+
cout<<"This is A. Nice to talk with you."<<endl;
39+
}
40+
41+
void UserB::reply(){
42+
cout<<"This is B. Nice to talk with you."<<endl;
43+
}
44+
45+
void UserC::reply(){
46+
cout<<"This is C. Nice to talk with you."<<endl;
47+
}

code/18_mediator/mediator_types.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <string>
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
class Colleague;
10+
class Mediator{
11+
public:
12+
virtual void registerColleague(Colleague* colleague) = 0;
13+
virtual Colleague* pick(string number) =0;
14+
};
15+
16+
class Colleague{
17+
public:
18+
Colleague(string number,Mediator* mediator):myNumber(number),callCeneter(mediator){}
19+
virtual void dial(string number);
20+
virtual void reply() =0;
21+
string getNumber();
22+
private:
23+
Mediator* callCeneter;
24+
string myNumber;
25+
};
26+
27+
class CallCenter : public Mediator{
28+
public:
29+
void registerColleague(Colleague* colleague);
30+
Colleague* pick(string number);
31+
private:
32+
vector<Colleague*> list;
33+
};
34+
35+
class UserA : public Colleague{
36+
public:
37+
UserA(string number,Mediator* mediator): Colleague(number,mediator){}
38+
void reply();
39+
};
40+
41+
class UserB : public Colleague{
42+
public:
43+
UserB(string number,Mediator* mediator): Colleague(number,mediator){}
44+
void reply();
45+
};
46+
47+
class UserC : public Colleague{
48+
public:
49+
UserC(string number,Mediator* mediator): Colleague(number,mediator){}
50+
void reply();
51+
};

code/18_mediator/result.png

4.12 KB
Loading

doc/18-迭代器.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,141 @@
3838
![figure17_iterator_case](img/figure17_iterator_case.png)
3939

4040
[code](../code/17_iterator)
41+
42+
43+
```c++
44+
// iterator_types.h
45+
46+
#pragma once
47+
#include <iostream>
48+
#include <string>
49+
#include <vector>
50+
51+
class Iterator{
52+
public:
53+
virtual int next()=0;
54+
virtual bool hasNext()=0;
55+
};
56+
57+
class Aggregate{
58+
public:
59+
virtual Iterator* createIterator()=0;
60+
};
61+
62+
class Post{
63+
public:
64+
Post(std::string cont):m_content(cont){
65+
66+
}
67+
void show(){
68+
std::cout<<m_content<<std::endl;
69+
}
70+
private:
71+
std::string m_content;
72+
};
73+
74+
class PostBox;
75+
class PostIterator: public Iterator{
76+
public:
77+
PostIterator(PostBox* bx);
78+
int next();
79+
bool hasNext();
80+
private:
81+
PostBox* box;
82+
int index;
83+
int total;
84+
};
85+
86+
87+
88+
class PostBox: public Aggregate{
89+
public:
90+
PostBox(std::vector<Post*> list):postList(list){
91+
92+
}
93+
Iterator* createIterator();
94+
void switchAndShow(int index);
95+
int totalSize();
96+
private:
97+
std::vector<Post*> postList;
98+
};
99+
```
100+
101+
```c++
102+
// iterator_types.cpp
103+
#include "iterator_types.h"
104+
105+
PostIterator::PostIterator(PostBox* bx):box(bx),index(0),total(1){
106+
if(bx){
107+
this->total = bx->totalSize();
108+
}
109+
}
110+
111+
int PostIterator::next(){
112+
int temp = this->index;
113+
this->index = (++this->index) % this->total;
114+
return temp;
115+
}
116+
117+
bool PostIterator::hasNext(){
118+
119+
return true;
120+
}
121+
122+
// PostBox
123+
Iterator* PostBox::createIterator(){
124+
125+
return new PostIterator(this);
126+
}
127+
128+
void PostBox::switchAndShow(int index){
129+
this->postList.at(index)->show();
130+
}
131+
132+
int PostBox::totalSize(){
133+
return this->postList.size();
134+
}
135+
```
136+
137+
```c++
138+
// client.cpp
139+
#include <iostream>
140+
141+
#include "iterator_types.h"
142+
143+
void mysleep(long count);
144+
145+
int main(){
146+
std::cout<<"Iterator pattern case:"<<std::endl;
147+
std::vector<Post*> list;
148+
list.push_back(new Post("Post 1"));
149+
list.push_back(new Post("Post 2"));
150+
list.push_back(new Post("Post 3"));
151+
list.push_back(new Post("Post 4"));
152+
list.push_back(new Post("Post 5"));
153+
154+
const int POST_SIZE = list.size();
155+
PostBox* box = new PostBox(list);
156+
Iterator* iter = box->createIterator();
157+
158+
while(iter->hasNext()){
159+
int postIndex = iter->next();
160+
box->switchAndShow(postIndex);
161+
if(postIndex == (POST_SIZE-1)){
162+
std::cout<< " --- " <<std::endl;
163+
}
164+
mysleep(50000000);
165+
}
166+
167+
return 0;
168+
}
169+
170+
void mysleep(long count){
171+
172+
for(long i=0;i<count;i++){
173+
174+
}
175+
}
176+
```
177+
178+
![result](../code/17_iterator/result.png)

0 commit comments

Comments
 (0)