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

worldblog發表於2007-12-15
Design Patterns: Solidify Your C# Application Architecture with Design Patterns中文版(尾篇二) (轉)[@more@]

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章