用設計模式固化你的C#程序(5-1)
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#示例:
using System;
abstract class State
{
public virtual void AddNickel(VendingMachine vm) { }
public virtual void AddDime(VendingMachine vm) { }
public virtual void AddQuarter(VendingMachine vm) { }
protected virtual void ChangeState(VendingMachine vm, State s)
{
vm.ChangeState(s);
}
}
......
/*以下是某次運行時輸出結果:
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!
*/