Design Patterns: Solidify Your C# Application Architecture with Design Patterns中文版(尾篇二) (轉)
Design Patterns: Solidify Your Application Architecture with Design Patterns中文版(尾篇二):namespace prefix = o ns = "urn:schemas--com::office" />
作者:Samir Bajaj
譯者:榮耀
【譯序:C#進階文章。譯者對Samir提供的C#例子進行了簡單整理(作者提供的某些程式碼在譯者的環境中無法透過編譯),並編寫了對應的C++示例,一併置於譯註中,以便讀者比對。譯文中所有C#、C++環境均為Microsoft 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
using namespace std;
void State::ChangeState(VendingMachine* vm, State* s)
{
vm->ChangeState(s);
}
VendingMachine::VendingMachine()
{
cout<
state = Start::Instance();
}
void VendingMachine::ChangeState(State* to)
{
state = to;
}
void VendingMachine::Vend()
{
//發飲料
cout<
}
void VendingMachine::AddNickel()
{
state->AddNickel(this);
}
void VendingMachine::AddDime()
{
state->AddDime(this);
}
void VendingMachine::AddQuarter()
{
state->AddQuarter(this);
}
State* Start::state = new Start();
State* Start::Instance()
{
// singleton 邏輯
cout<
return state;
}
void Start::AddNickel(VendingMachine* vm)
{
ChangeState(vm, Five::Instance());
}
void Start::AddDime(VendingMachine* vm)
{
ChangeState(vm, Ten::Instance());
}
void Start::AddQuarter(VendingMachine* vm)
{
vm->Vend();
}
State* Five::state = new Five();
State* Five::Instance()
{
// singleton邏輯
cout<
return state;
}
void Five::AddNickel(VendingMachine* vm)
{
ChangeState(vm, Ten::Instance());
}
void Five::AddDime(VendingMachine* vm)
{
ChangeState(vm, Fifteen::Instance());
}
void Five::AddQuarter(VendingMachine* vm)
{
vm->Vend();
ChangeState(vm, Start::Instance()); // no change returned :-)
}
State* Ten::state = new Ten();
State* Ten::Instance()
{
// singleton 邏輯
cout<
return state;
}
void Ten::AddNickel(VendingMachine* vm)
{
ChangeState(vm, Fifteen::Instance());
}
void Ten::AddDime(VendingMachine* vm)
{
ChangeState(vm, Twenty::Instance());
}
void Ten::AddQuarter(VendingMachine* vm)
{
vm->Vend();
ChangeState(vm, Start::Instance()); // no change returned :-)
}
State* Fifteen::state = new Fifteen();
State* Fifteen::Instance()
{
// singleton 邏輯
cout<
return state;
}
void Fifteen::AddNickel(VendingMachine* vm)
{
ChangeState(vm, Twenty::Instance());
}
void Fifteen::AddDime(VendingMachine* vm)
{
vm->Vend();
ChangeState(vm, Start::Instance());
}
void Fifteen::AddQuarter(VendingMachine* vm)
{
vm->Vend();
ChangeState(vm, Start::Instance()); // no change returned :-)
}
State* Twenty::state = new Twenty();
State* Twenty::Instance()
{
// singleton 邏輯
cout<
return state;
}
void Twenty::AddNickel(VendingMachine* vm)
{
vm->Vend();
ChangeState(vm, Start::Instance());
}
void Twenty::AddDime(VendingMachine* vm)
{
vm->Vend();
ChangeState(vm, Start::Instance());
}
void Twenty::AddQuarter(VendingMachine* vm)
{
vm->Vend();
ChangeState(vm, Start::Instance()); // no change returned :-)
}
int _tmain(int argc, _TCHAR* argv[])
{
int coin = 0;
VendingMachine* vm = new VendingMachine();
int i = 0;
while (i < 10) //【譯註:對應的C#程式碼是while(true),為避免洩漏問題,改為如此。否則,永遠都不會後面四行@程式碼】
{
cout<
cin>>coin;
switch (coin)
{
case 5:
vm->AddNickel();
break;
case 10:
vm->AddDime();
break;
case 25:
vm->AddQuarter();
break;
default:
break;
}
i++;
}
delete Start::Instance();//@
delete Five::Instance();//@
delete Ten::Instance();//@
delete Fifteen::Instance();//@
return 0;
}
/*以下是某次執行時輸出結果:
The Vending Machine is now online: product costs 25c
Credit: 0c
Insert a coin <5, 10, 25>: 5
Credit: 5c
Insert a coin <5, 10, 25>: 10
Credit: 15c
Insert a coin <5, 10, 25>: 5
Credit: 20c
Insert a coin <5, 10, 25>: 5
Dispensing product...Thank you!
*/
】
結論
設計吸取了在面向設計中常見問題的解決方案的多年之精華。不管專案規模如何,它們為大多數人員碰到的問題提供了答案。C#提高了程式設計師的生產力,它加入了可以促進物件導向設計的特性,並減少了開發人員手工勞動負擔。二者結合,無往不利。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-993613/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Design Patterns: Solidify Your C# Application Architecture with Design Patterns中文版(尾篇一) (轉)SolidC#APP
- Design Patterns: Solidify Your C# Application Architecture with Design Patterns中文版(中篇) (轉)SolidC#APP
- Design Patterns: Solidify Your C# Application Architecture with Design Patterns中文版(下篇) (轉)SolidC#APP
- Design Patterns Java CompanionJava
- 設計模式(Design Patterns)筆記 (轉)設計模式筆記
- Design Patterns 之工廠模式模式
- 設計模式(Design Patterns)筆記之二:Adapter (轉)設計模式筆記APT
- Design Patterns in Android:裝飾模式Android模式
- Design Patterns in Android:責任鏈模式Android模式
- design patterns - 從頭講解MVC模式MVC模式
- Cloud Design Patterns Book Reading(undone)Cloud
- Web UI Design Patterns 2014WebUI
- 設計模式(Design Patterns)筆記之一:Abstract Factory (轉)設計模式筆記
- 設計模式(Design Patterns)筆記之三:Bridge (轉)設計模式筆記
- 設計模式(Design Patterns)工廠方法模式(Factory Method)設計模式
- 為什麼要學習設計模式(Design Patterns) - 軟體設計大師之路 (轉)設計模式
- Application Design BasicsAPP
- React PatternsReact
- Observer Design Pattern in C#! (轉)ServerC#
- 為什麼要學習設計模式(Design Patterns) - 怎樣成為軟體設計大師 (轉)設計模式
- No model,no patterns ,then no frameworkFramework
- Digital PatternsGit
- Concurrency Patterns in GoGo
- Glob Patterns匹配模式使用模式
- Patterns and Frameworks (my first topic)Framework
- 《設計模式——可複用物件導向軟體的基礎》學習筆記(1.4)The catalog of design patterns...設計模式物件筆記
- 《設計模式——可複用物件導向軟體的基礎》學習筆記(1.2)Design patterns in Smalltalk MVC...設計模式物件筆記MVC
- Advanced-react-patterns(2)React
- Java 21 新特性:Record PatternsJava
- 閒侃Domain Logic PatternsAI
- Infrastructure tooling patternsASTStruct
- 《設計模式——可複用物件導向軟體的基礎》學習筆記(1.3)Describing design patterns...設計模式物件筆記
- Design with static members (轉)
- A Look Inside J2EE Patterns(2) (轉)IDE
- A Look Inside J2EE Patterns(3) (轉)IDE
- A Look Inside J2EE Patterns(4) (轉)IDE
- A Look Inside J2EE Patterns(5) (轉)IDE
- Enterprise Integration Patterns with BizTalk ServerServer