Dota design mode-command

Source: Internet
Author: User
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
{
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.