Design Patterns: Solidify Your C # Application Architecture with Design Patterns Chinese Edition (Tail 2)
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]
C example:
[Translation: Since this code is relatively complicated, the category declaration and definition is separated.
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 Add Nickel (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 add nickel ();
Void adddime ();
Void addquarter ();
}
Class Start: Public State
{
Private:
STATIC state * state;
PUBLIC:
Static state * instance ();
Void Add Nickel (VendingMachine * VM);
Void AddDime (VendingMachine * VM);
Void Addquarter (VendingMachine * VM);
}
Class Five: Public State
{
Private:
STATIC state * state;
PUBLIC:
Static state * instance ();
Void Add Nickel (VendingMachine * VM);
Void AddDime (VendingMachine * VM);
Void Addquarter (VendingMachine * VM);
}
Class Ten: Public State
{
Private:
STATIC state * state;
PUBLIC:
Static state * instance ();
Void Add Nickel (VendingMachine * VM);
Void AddDime (VendingMachine * VM);
Void Addquarter (VendingMachine * VM);
}
Class Fifteen: Public State
{
Private:
STATIC state * state;
PUBLIC:
Static state * instance ();
Void Add Nickel (VendingMachine * VM);
Void adddime (vendingmachine * vm); void addquarter (VendingMachine * VM);
}
Class TWENTY: PUBLIC STATE
{
Private:
STATIC state * state;
PUBLIC:
Static state * instance ();
Void Add Nickel (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" << ENDL;
State = start :: instance ();
}
Void VendingMachine :: ChangeState (state * to)
{
State = TO;
}
Void VendingMachine :: vend ()
{
// send beverage
Cout << "Dispensing Product ... Thank you!" << Endl;
}
Void VendingMachine :: add nickel ()
{
State-> Add Nickel (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" << endl;
Return State;
}
Void Start :: Add Nickel (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" << endl;
Return State;
}
Void Five :: Add Nickel (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" << endl;
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 logic
COUT << "CREDIT: 15C" << endl;
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 logic
COUT << "CREDIT: 20C" << endl;
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) // [Translation: The corresponding C # code is while (TRUE), to avoid memory leakage problems, change to this. Otherwise, it will never perform four lines @ code] {
COUT << "Insert a Coin (5, 10, 25):";
CIN >> COIN;
Switch (coin)
{
Case 5:
VM-> add nickel ();
Break;
Case 10:
VM-> adddime ();
Break;
Case 25:
VM-> addquarter ();
Break;
DEFAULT:
Break;
}
i ;
}
Delete start :: instance (); @ @ @ @ s
Delete FIVE :: Instance (); @ @ @ @
Delete Ten :: instance (); @ @
Delete fifteen :: instance (); @ @
Return 0;
}
/ * 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!
* /
】
in conclusion
Design patterns take the essence of multi-year experience in solutions that are commonly seen in object-oriented software design. Regardless of the size of the project, they provide answers to the issues encountered by most software developers. C # improves the productivity of the programmer, which has added the characteristics that can promote object-oriented design and reduce the development of manual labor. The combination of the two is not good.