Use the design mode to solidify your C # Program (6-2)
Design Patterns: Solidify Your C # Application Architecture with Design Patterns Chinese edition (Part 2)
Author: Samir Bajaj
Translator: glory
State. h
# Include "stdafx. h ";
Class State;
Class VendingMachine;
Class Start;
Class Five;
Class Ten;
Class internal tee;
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 * );
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 deleteen: 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 <"The Vending Machine is now online: product costs 25c" <state = Start: Instance ();
}
Void VendingMachine: ChangeState (State *)
{
State =;
}
Void VendingMachine: Vend ()
{
File: // sendBeverage
Cout <"Dispensing product... Thank you! "<}
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 Logic
Cout <"Credit: 0c" <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 Logic
Cout <"Credit: 5c" <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 Logic
Cout <"Credit: 10c" <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 * deleteen: Instance ()
{
// Singleton Logic
Cout <"Credit: 15c" <return state;
}
Void deleteen: AddNickel (VendingMachine * vm)
{
ChangeState (vm, Twenty: Instance ());
}
Void deleteen: AddDime (VendingMachine * vm)
{
Vm-> Vend ();
ChangeState (vm, Start: Instance ());
}
Void deleteen: AddQuarter (VendingMachine * vm)
{
Vm-> Vend ();
ChangeState (vm, Start: Instance (); // no change returned :-)
}
State * Twenty: state = new Twenty ();
State * Twenty: Instance ()
{
// Singleton Logic
Cout <"Credit: 20c" <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)File ://【The corresponding C # code is while (true). This is the case to avoid Memory leakage. Otherwise, the last four lines of @ code will never be executed]
{
Cout <"Insert a coin (5, 10, 25 ):";
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 deleteen: Instance ();//@
Return 0;
}
/* 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!
*/
]