Skip to content

Commit e7c4c07

Browse files
committed
add code in doc
1 parent 649f3f4 commit e7c4c07

File tree

2 files changed

+257
-1
lines changed

2 files changed

+257
-1
lines changed

doc/20-备忘录.md

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,132 @@
3737

3838
[code](../code/19_memento)
3939

40-
40+
```c++
41+
// memento_types.h
42+
#ifndef __MEMENTO_TYPES_H__
43+
#define __MEMENTO_TYPES_H__
44+
45+
#include <iostream>
46+
#include <string>
47+
#include <stack>
48+
using namespace std;
49+
50+
class Step
51+
{
52+
public:
53+
Step(std::string state) : state_(state) {}
54+
~Step() {}
55+
inline std::string getState()
56+
{
57+
return state_;
58+
}
59+
60+
private:
61+
std::string state_;
62+
};
63+
64+
class Player
65+
{
66+
public:
67+
Player();
68+
~Player();
69+
Step *doAction(std::string state);
70+
void applyAction(Step *step);
71+
72+
private:
73+
std::string state_;
74+
};
75+
76+
class StepsManager
77+
{
78+
public:
79+
StepsManager();
80+
~StepsManager();
81+
void push(Step *step);
82+
Step *pop();
83+
84+
private:
85+
std::stack<Step *> mementos;
86+
};
87+
88+
#endif // __MEMENTO_TYPES_H__
89+
```
90+
91+
```c++
92+
// memento_types.cpp
93+
#include "memento_types.h"
94+
95+
Player::Player()
96+
{
97+
}
98+
99+
Player::~Player()
100+
{
101+
}
102+
103+
Step *Player::doAction(std::string state)
104+
{
105+
this->state_ = state;
106+
Step *step = new Step(this->state_);
107+
cout << "Do step:" << this->state_ << endl;
108+
return step;
109+
}
110+
111+
void Player::applyAction(Step *step)
112+
{
113+
if (step)
114+
{
115+
this->state_ = step->getState();
116+
cout << "Back step:" << this->state_ << endl;
117+
}
118+
}
119+
120+
StepsManager::StepsManager()
121+
{
122+
}
123+
124+
StepsManager::~StepsManager()
125+
{
126+
}
127+
128+
void StepsManager::push(Step *step)
129+
{
130+
if (step)
131+
{
132+
mementos.push(step);
133+
}
134+
}
135+
136+
Step *StepsManager::pop()
137+
{
138+
Step *step = NULL;
139+
if (mementos.empty())
140+
return step;
141+
step = mementos.top();
142+
mementos.pop();
143+
return step;
144+
}
145+
146+
```
147+
148+
```c++
149+
#include "memento_types.h"
150+
151+
int main()
152+
{
153+
Player player1;
154+
StepsManager stepManager;
155+
cout << "-----------" << endl;
156+
stepManager.push(player1.doAction("车2平5"));
157+
stepManager.push(player1.doAction("兵3进1"));
158+
stepManager.push(player1.doAction("兵4平1"));
159+
160+
player1.applyAction(stepManager.pop());
161+
player1.applyAction(stepManager.pop());
162+
player1.applyAction(stepManager.pop());
163+
cout << "-----------" << endl;
164+
return 0;
165+
}
166+
167+
```
41168
![result](../code/19_memento/result.png)

doc/21-观察者.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,133 @@
3838

3939
[code](../code/20_observer)
4040

41+
```c++
42+
// observer_types.h
43+
#ifndef __OBSERVER_TYPES_H__
44+
#define __OBSERVER_TYPES_H__
45+
46+
#include <iostream>
47+
#include <string>
48+
#include <vector>
49+
using namespace std;
50+
51+
class Buyer
52+
{
53+
public:
54+
virtual ~Buyer() {}
55+
virtual void action() = 0;
56+
};
57+
58+
class Product
59+
{
60+
public:
61+
Product() : amount(0) {}
62+
virtual ~Product() {}
63+
virtual void addBuyer(Buyer *buyer) = 0;
64+
virtual void removeBuyer(Buyer *buyer) = 0;
65+
virtual void notifyBuyers() = 0;
66+
void setAmount(int amount)
67+
{
68+
this->amount = amount;
69+
if (this->amount > 0)
70+
{
71+
std::cout << "Goods arrived!" << std::endl;
72+
notifyBuyers();
73+
}
74+
}
75+
76+
protected:
77+
int amount;
78+
};
79+
80+
class IPhoneX : public Product
81+
{
82+
public:
83+
~IPhoneX();
84+
void addBuyer(Buyer *buyer);
85+
void removeBuyer(Buyer *buyer);
86+
void notifyBuyers();
87+
88+
private:
89+
vector<Buyer *> buyers;
90+
};
91+
92+
class BuyerA : public Buyer
93+
{
94+
public:
95+
void action();
96+
};
97+
98+
class BuyerB : public Buyer
99+
{
100+
public:
101+
void action();
102+
};
103+
104+
#endif // __OBSERVER_TYPES_H__
105+
```
106+
```c++
107+
// observer_types.cpp
108+
#include "observer_types.h"
109+
110+
void IPhoneX::addBuyer(Buyer *buyer)
111+
{
112+
buyers.push_back(buyer);
113+
}
114+
115+
void IPhoneX::removeBuyer(Buyer *buyer)
116+
{
117+
for (vector<Buyer *>::iterator itr = buyers.begin();
118+
itr != buyers.end(); itr++)
119+
{
120+
if (*itr == buyer)
121+
{
122+
buyers.erase(itr);
123+
return;
124+
}
125+
}
126+
}
127+
128+
void IPhoneX::notifyBuyers()
129+
{
130+
for (vector<Buyer *>::iterator itr = buyers.begin();
131+
itr != buyers.end();
132+
itr++)
133+
{
134+
(*itr)->action();
135+
}
136+
}
137+
138+
void BuyerA::action()
139+
{
140+
std::cout << "A: OK I will buy it." << std::endl;
141+
}
142+
143+
void BuyerB::action()
144+
{
145+
std::cout << "B: Take another look." << std::endl;
146+
}
147+
148+
IPhoneX::~IPhoneX()
149+
{
150+
buyers.clear();
151+
}
152+
153+
```
154+
```c++
155+
// client.cpp
156+
#include "observer_types.h"
157+
158+
int main(){
159+
Product* ihponex = new IPhoneX();
160+
Buyer* buyerA = new BuyerA();
161+
Buyer* buyerB = new BuyerB();
162+
163+
ihponex->addBuyer(buyerA);
164+
ihponex->addBuyer(buyerB);
165+
166+
ihponex->setAmount(100);
167+
return 1;
168+
}
169+
```
41170
![result](../code/20_observer/result.png)

0 commit comments

Comments
 (0)