Design Patterns: Solidify Your C# Application Architecture with Design Patterns中文版(尾篇二)

** Design Patterns: Solidify Your C# Application Architecture with Design Patterns中文版(尾篇二) **

作者: Samir Bajaj

译者:荣耀

【译序: C#进阶文章。译者对Samir提供的C#例子进行了简单整理(作者提供的某些代码在译者的环境中无法通过编译),并编写了对应的C++示例,一并置于译注中,以便读者比对。 译文中所有 C#、C++程序调试环境均为Microsoft Visual Studio.NET 7.0 Beta2 】

** C++示例: ** 【译注:由于此例代码相对复杂,故把类的声明和定义分离开 J 】 ** **

state.h

#include "stdafx.h";

class State;

class VendingMachine;

class Start;

class Five;

class Ten;

class Fiftee;

class Twenty;

class State

{

public:

virtual void AddNickel(VendingMachine* vm) { }

virtual void AddDime(VendingMachine* vm) { }

virtual void AddQuarter(VendingMachine* vm) { }

protected:

virtual void ChangeState(VendingMachine* vm, State* s);

};

class VendingMachine

{

private:

State* state;

public:

VendingMachine();

void ChangeState(State* to);

void Vend();

void AddNickel();

void AddDime();

void AddQuarter();

};

class Start : public State

{

private:

static State* state;

public:

static State* Instance();

void AddNickel(VendingMachine* vm);

void AddDime(VendingMachine* vm);

void AddQuarter(VendingMachine* vm);

};

class Five : public State

{

private:

static State* state;

public:

static State* Instance();

void AddNickel(VendingMachine* vm);

void AddDime(VendingMachine* vm);

void AddQuarter(VendingMachine* vm);

};

class Ten : public State

{

private:

static State* state;

public:

static State* Instance();

void AddNickel(VendingMachine* vm);

void AddDime(VendingMachine* vm);

void AddQuarter(VendingMachine* vm);

};

class Fifteen : public State

{

private:

static State* state;

public:

static State* Instance();

void AddNickel(VendingMachine* vm);

void AddDime(VendingMachine* vm);

void AddQuarter(VendingMachine* vm);

};

class Twenty : public State

{

private:

static State* state;

public:

static State* Instance();

void AddNickel(VendingMachine* vm);

void AddDime(VendingMachine* vm);

void AddQuarter(VendingMachine* vm);

** state.cpp **

#include "stdafx.h";

#include "test.h";

#include

  1<iostream>
  2
  3using namespace std; 
  4
  5void State::ChangeState(VendingMachine* vm, State* s) 
  6
  7{ 
  8
  9vm-&gt;ChangeState(s); 
 10
 11} 
 12
 13VendingMachine::VendingMachine() 
 14
 15{ 
 16
 17cout&lt;&lt;"The Vending Machine is now online: product costs 25c"&lt;<endl; cout<<"dispensing="" product...thank="" state="to;" state-="" to)="" vendingmachine::addnickel()="" vendingmachine::changestate(state*="" vendingmachine::vend()="" void="" you!"<<endl;="" {="" }="" 发饮料="">AddNickel(this); 
 18
 19} 
 20
 21void VendingMachine::AddDime() 
 22
 23{ 
 24
 25state-&gt;AddDime(this); 
 26
 27} 
 28
 29void VendingMachine::AddQuarter() 
 30
 31{ 
 32
 33state-&gt;AddQuarter(this); 
 34
 35} 
 36
 37State* Start::state = new Start(); 
 38
 39State* Start::Instance() 
 40
 41{ 
 42
 43// singleton 逻辑 
 44
 45cout&lt;&lt;"Credit: 0c"&lt;<endl; changestate(vm,="" five::instance());="" return="" start::adddime(vendingmachine*="" start::addnickel(vendingmachine*="" start::addquarter(vendingmachine*="" state;="" ten::instance());="" vm)="" vm-="" void="" {="" }="">Vend(); 
 46
 47} 
 48
 49State* Five::state = new Five(); 
 50
 51State* Five::Instance() 
 52
 53{ 
 54
 55// singleton逻辑 
 56
 57cout&lt;&lt;"Credit: 5c"&lt;<endl; changestate(vm,="" fifteen::instance());="" five::adddime(vendingmachine*="" five::addnickel(vendingmachine*="" five::addquarter(vendingmachine*="" return="" state;="" ten::instance());="" vm)="" vm-="" void="" {="" }="">Vend(); 
 58
 59ChangeState(vm, Start::Instance());  // no change returned :-) 
 60
 61} 
 62
 63State* Ten::state = new Ten(); 
 64
 65State* Ten::Instance() 
 66
 67{ 
 68
 69// singleton 逻辑 
 70
 71cout&lt;&lt;"Credit: 10c"&lt;<endl; changestate(vm,="" fifteen::instance());="" return="" state;="" ten::adddime(vendingmachine*="" ten::addnickel(vendingmachine*="" ten::addquarter(vendingmachine*="" twenty::instance());="" vm)="" vm-="" void="" {="" }="">Vend(); 
 72
 73ChangeState(vm, Start::Instance());  // no change returned :-) 
 74
 75} 
 76
 77State* Fifteen::state = new Fifteen(); 
 78
 79State* Fifteen::Instance() 
 80
 81{ 
 82
 83// singleton 逻辑 
 84
 85cout&lt;&lt;"Credit: 15c"&lt;<endl; changestate(vm,="" fifteen::adddime(vendingmachine*="" fifteen::addnickel(vendingmachine*="" return="" state;="" twenty::instance());="" vm)="" vm-="" void="" {="" }="">Vend(); 
 86
 87ChangeState(vm, Start::Instance()); 
 88
 89} 
 90
 91void Fifteen::AddQuarter(VendingMachine* vm) 
 92
 93{ 
 94
 95vm-&gt;Vend(); 
 96
 97ChangeState(vm, Start::Instance());  // no change returned :-) 
 98
 99} 
100
101State* Twenty::state = new Twenty(); 
102
103State* Twenty::Instance() 
104
105{ 
106
107// singleton 逻辑 
108
109cout&lt;&lt;"Credit: 20c"&lt;<endl; return="" state;="" twenty::addnickel(vendingmachine*="" vm)="" vm-="" void="" {="" }="">Vend(); 
110
111ChangeState(vm, Start::Instance()); 
112
113} 
114
115void Twenty::AddDime(VendingMachine* vm) 
116
117{ 
118
119vm-&gt;Vend(); 
120
121ChangeState(vm, Start::Instance()); 
122
123} 
124
125void Twenty::AddQuarter(VendingMachine* vm) 
126
127{ 
128
129vm-&gt;Vend(); 
130
131ChangeState(vm, Start::Instance());  // no change returned :-) 
132
133} 
134
135int _tmain(int argc, _TCHAR* argv[]) 
136
137{ 
138
139int coin = 0; 
140
141VendingMachine* vm = new VendingMachine(); 
142
143int i = 0; 
144
145while (i &lt; 10) //【译注:对应的C#代码是while(true),为避免内存泄漏问题,改为如此。否则,永远都不会执行后面四行@代码】 
146
147{ 
148
149cout&lt;&lt;"Insert a coin (5, 10, 25): "; 
150
151cin&gt;&gt;coin; 
152
153switch (coin) 
154
155{ 
156
157case 5: 
158
159vm-&gt;AddNickel(); 
160
161break; 
162
163case 10: 
164
165vm-&gt;AddDime(); 
166
167break; 
168
169case 25: 
170
171vm-&gt;AddQuarter(); 
172
173break; 
174
175default: 
176
177break; 
178
179} 
180
181i++; 
182
183} 
184
185delete Start::Instance();//@ 
186
187delete Five::Instance();//@ 
188
189delete Ten::Instance();//@ 
190
191delete Fifteen::Instance();//@ 
192
193return 0; 
194
195} 
196
197/*以下是某次运行时输出结果: 
198
199The Vending Machine is now online: product costs 25c 
200
201Credit: 0c 
202
203Insert a coin &lt;5, 10, 25&gt;: 5 
204
205Credit: 5c 
206
207Insert a coin &lt;5, 10, 25&gt;: 10 
208
209Credit: 15c 
210
211Insert a coin &lt;5, 10, 25&gt;: 5 
212
213Credit: 20c 
214
215Insert a coin &lt;5, 10, 25&gt;: 5 
216
217Dispensing product...Thank you! 
218
219*/ 
220
221222
223** 结论  **
224
225设计模式吸取了在面向对象软件设计中常见问题的解决方案的多年经验之精华。不管项目规模如何,它们为大多数软件开发人员碰到的问题提供了答案。C#提高了程序员的生产力,它加入了可以促进面向对象设计的特性,并减少了开发人员手工劳动负担。二者结合,无往不利。</endl;></endl;></endl;></endl;></endl;></endl;></iostream>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus