Design Patterns: Solidify Your C # Application Architecture With Design Patterns Chinese Edition (Tail 1)
Author: Samir Bajaj
Translator: glory
[Decoration: C # advanced article. The translator made a simple finishing of the C # example provided by Samir (the author provided in the translator's environment) and writes the corresponding C example, and placed in the translation. All C #, C program debugging environments in the translation are Microsoft Visual Studio.Net 7.0 Beta2]
[Translation: The following is a complete example of the State mode
C # example:
Using system;
Abstract Class State
{
Public Virtual Void Add Nickel (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);
}
}
Class VendingMachine
{
PRIVATE STATE;
Public vendingmachine ()
{
Console.writeline ("The Vending Machine Is Now Online: Product Costs 25c");
State = start.instance ();
}
Public Void ChangeState (State To)
{
State = TO;
}
Public void vend ()
{
// send beverage
Console.WriteLine ("Dispensing Product ... Thank you!");
}
Public void add nickel ()
{
State.adDnickel (this);
}
Public void adddime ()
{
State.adddime (this);
}
Public void addquarter ()
{
State.addquarter (this);
}
}
Class Start: State
{
Private static state statness = new start ();
Private start ()
{
}
Public Static State Instance ()
{
// Singleton logic
Console.writeline ("CRedit: 0C");
Return State;
}
Public Override Void Add Nickel (VendingMachine VM)
{
ChangeState (VM, Five.instance ());
}
Public Override Void Adddime (VendingMachine VM)
{
ChangeState (VM, Ten.instance ());
}
Public Override Void Addquarter (VendingMachine VM)
{
vm.vend ();
}
}
Class Five: State
{
Private static state state = new five ();
Private file ()
{
}
Public static state instance () {
// Singleton logic
Console.writeline ("CREDIT: 5C");
Return State;
}
Public Override Void Add Nickel (VendingMachine VM)
{
ChangeState (VM, Ten.instance ());
}
Public Override Void Adddime (VendingMachine VM)
{
ChangeState (VM, Fifteen.instance ());
}
Public Override Void Addquarter (VendingMachine VM)
{
vm.vend ();
ChangeState (VM, Start.instance ()); // no change returned :-)
}
}
Class Ten: State
{
Private static state state = new ten ();
private ten ()
{
}
Public Static State Instance ()
{
// Singleton logic
Console.writeline ("CREDIT: 10C");
Return State;
}
Public Override Void Add Nickel (VendingMachine VM)
{
ChangeState (VM, Fifteen.instance ());
}
Public Override Void Adddime (VendingMachine VM)
{
ChangeState (VM, TWENTY.INSTANCE ());
}
Public Override Void Addquarter (VendingMachine VM)
{
vm.vend ();
ChangeState (VM, Start.instance ()); // no change returned :-)
}
}
Class Fifteen: State
{
Private static state statness = new fifteen ();
Private fifteen ()
{
}
Public Static State Instance ()
{
// Singleton logic
Console.writeLine ("CREDIT: 15C");
Return State;
}
Public Override Void Add Nickel (VendingMachine VM)
{
ChangeState (VM, TWENTY.INSTANCE ());
}
Public Override Void Adddime (VendingMachine VM)
{
vm.vend ();
ChangeState (VM, Start.instance ());
}
Public Override Void Addquarter (VendingMachine VM)
{
vm.vend ();
ChangeState (VM, Start.instance ()); // no change returned :-)
}
}
Class TWENTY: STATE
{
Private static state statness = new twenty ();
Private twenty ()
{
}
Public Static State Instance ()
{
// Singleton logic
Console.writeLine ("CREDIT: 20C");
Return State;
}
Public Override Void Add Nickel (VendingMachine VM) {
vm.vend ();
ChangeState (VM, Start.instance ());
}
Public Override Void Adddime (VendingMachine VM)
{
vm.vend ();
ChangeState (VM, Start.instance ());
}
Public Override Void Addquarter (VendingMachine VM)
{
vm.vend ();
ChangeState (VM, Start.instance ()); // no change returned :-)
}
}
Class Application
{
Public static void main ()
{
INT COIN = 0;
String infut = null;
Vendingmachine vm = new vendingmachine ();
While (True)
{
Console.Write ("INSERT A COIN (5, 10, 25):");
INPUT = console.readline ();
COIN = Int32.Parse (Input);
Switch (coin)
{
Case 5:
VM.Addnickel ();
Break;
Case 10:
vm.adddime ();
Break;
Case 25:
vm.addquarter ();
Break;
DEFAULT:
Break;
}
}
}
}
/ * The following is a certain runtime output result:
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!
* /