Use the design mode to solidify your C # Program (5-1)
Design Patterns: Solidify Your C # Application Architecture with Design Patterns Chinese edition (Part 1)
Author: Samir Bajaj
Translator: glory
The following is a complete example of the state mode.
C # example:
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 );
}
}
Class VendingMachine
{
Private State state;
Public VendingMachine ()
{
Console. WriteLine ("The Vending Machine is now online: product costs 25c ");
State = Start. Instance ();
}
Public void ChangeState (State)
{
State =;
}
Public void Vend ()
{
// Send a drink
Console. WriteLine ("Dispensing product... Thank you! ");
}
Public void AddNickel ()
{
State. AddNickel (this );
}
Public void AddDime ()
{
State. AddDime (this );
}
Public void AddQuarter ()
{
State. AddQuarter (this );
}
}
Class Start: State
{
Private static State state = new Start ();
Private Start ()
{
}
Public static State Instance ()
{
// Singleton Logic
Console. WriteLine ("Credit: 0c ");
Return state;
}
Public override void AddNickel (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 Five ()
{
}
Public static State Instance ()
{
// Singleton Logic
Console. WriteLine ("Credit: 5c ");
Return state;
}
Public override void AddNickel (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 AddNickel (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 state = new Fifteen ();
Private deleteen ()
{
}
Public static State Instance ()
{
// Singleton Logic
Console. WriteLine ("Credit: 15c ");
Return state;
}
Public override void AddNickel (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 state = new Twenty ();
Private Twenty ()
{
}
Public static State Instance ()
{
// Singleton Logic
Console. WriteLine ("Credit: 20c ");
Return state;
}
Public override void AddNickel (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 input = 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 the output result of a running task:
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!
*/