The command mode seems simpler, but it is still very complicated.
Code It is relatively simple. All commands only need to inherit the icommand interface.
Public Interface Icommand
{
Object Execute ();
}
For the specific command class, see the complete code. The command object defines the operation object in the constructor. The execute method calls the object defined in the constructor. In this example, the Undo implementation is missing. You only need to record the command when executing the command and perform a reverse operation when executing the undo command.
UML diagram:
Test code:
Dotapatternlibrary. Command. controlui control = new dotapatternlibrary. Command. controlui ();
Dotapatternlibrary. Command. peasent = new dotapatternlibrary. Command. peasent ();
Dotapatternlibrary. Command. icommand command = new dotapatternlibrary. Command. buildaltar (peasent );
Control. addcommand ("ba", command );
Command = new dotapatternlibrary. Command. buildbarracks (peasent );
Control. addcommand ("BB", command );
Control. pressbutton ("ba ");
Object o = control. pressbutton ("BB ");
Dotapatternlibrary. Command. Barracks bbobj = O as dotapatternlibrary. Command. barracks;
Command = new dotapatternlibrary. Command. productsoldier (bbobj );
Control. addcommand ("Ps", command );
Control. pressbutton ("Ps ");
Complete code: Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. collections;
UsingDotacommon;
Namespace Dotapatternlibrary. Command
{
Public Class Controlui
{
Private Hashtable commands;
Public Controlui ()
{
Commands = New Hashtable ();
}
Public Void Addcommand ( String Button, icommand command)
{
Commands. Add (button, command );
}
Public Object pressbutton ( string button)
{< br> return (icommand) commands [button]). execute ();
}< BR >}
Public interface icommand
{< br> Object execute ();
}
Public class buildaltar: icommand
{< br> peasent;
Public buildaltar (peasent)
{< br> This . peasent = peasent;
}
# RegionIcommand Member
Public ObjectExecute ()
{
ReturnPeasent. buildaltar ();
}
# Endregion
}
Public class unbuildaltar: icommand
{< br> peasent;
Public unbuildaltar (peasent)
{< br> This . peasent = peasent;
}
# RegionIcommand Member
Public ObjectExecute ()
{
ReturnPeasent. unbuildaltar ();
}
# Endregion
}
Public class buildbarracks: icommand
{< br> peasent;
Public buildbarracks (peasent)
{< br> This . peasent = peasent;
}
# RegionIcommand Member
Public ObjectExecute ()
{
ReturnPeasent. buildbarracks ();
}
# Endregion
}
Public ClassUnbuildbarracks: icommand
{
Peasent;
PublicUnbuildbarracks (peasent)
{
This. Peasent=Peasent;
}
# RegionIcommand Member
Public ObjectExecute ()
{
ReturnPeasent. unbuildbarracks ();
}
# Endregion
}
Public Class Productsoldier: icommand
{
Barracks barracks;
Public Productsoldier (barracks)
{
This . Barracks = Barracks;
}
# Region Icommand Member
Public ObjectExecute ()
{
ReturnBarracks. productsoldier ();
}
# Endregion
}
Public Class Unproductsoldier: icommand
{
Barracks barracks;
Public Unproductsoldier (barracks)
{
This . Barracks = Barracks;
}
# Region Icommand Member
Public ObjectExecute ()
{
ReturnBarracks. unproductsoldier ();
}
# Endregion
}
Public Class Peasent
{
Public Alltar buildaltar ()
{
Landpyform. Form. outputresult ( " Buildaltar " );
Return New Alltar ();
}
Public ObjectUnbuildaltar ()
{
Landpyform. Form. outputresult ("Undo buildaltar");
Return Null;
}
Public Object buildbarracks ()
{
landpyform. form. outputresult ( " buildbarracks " );
return New barracks ();
}
Public ObjectUnbuildbarracks ()
{
Landpyform. Form. outputresult ("Undo buildbarracks");
Return Null;
}
}
Public Class Alltar
{
Public Void Producthero ()
{
Landpyform. Form. outputresult ( " Producthero " );
}
Public Void Unproducthero ()
{
Landpyform. Form. outputresult ( " Undo producthero " );
}
}
Public Class Barracks
{
Public Object Productsoldier ()
{
Landpyform. Form. outputresult ( " Productsoldier " );
Return New SOLDIER ();
}
Public Object Unproductsoldier ()
{
Landpyform. Form. outputresult ( " Undo productsoldier " );
Return Null ;
}
}
Public ClassSoldier
{
}
}