DOTA版设计模式——命令
命令模式看起来好像简单些,实际还是十分复杂的。
代码比较简单,所有的Command只要继承了ICommand接口即可。
public interface ICommand
{
object Execute();
}
具体的Command类见完整代码。Command对象在构造函数中定义操作的对象,Execute方法调用构造函数中定义的对象。本示例缺少了Undo的实现,只要在执行命令时把命令记录下来,执行Undo时进行反操作即可。
UML图:
测试代码:
DotaPatternLibrary.Command.ControlUI control = new DotaPatternLibrary.Command.ControlUI();
DotaPatternLibrary.Command.Peasent 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");
完整代码:
代码比较简单,所有的Command只要继承了ICommand接口即可。
public interface ICommand
{
object Execute();
}
具体的Command类见完整代码。Command对象在构造函数中定义操作的对象,Execute方法调用构造函数中定义的对象。本示例缺少了Undo的实现,只要在执行命令时把命令记录下来,执行Undo时进行反操作即可。
UML图:
测试代码:
DotaPatternLibrary.Command.ControlUI control = new DotaPatternLibrary.Command.ControlUI();
DotaPatternLibrary.Command.Peasent 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");
完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DotaCommon;
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)
{
return ((ICommand)commands[button]).Execute();
}
}
public interface ICommand
{
object Execute();
}
public class BuildAltar : ICommand
{
Peasent peasent;
public BuildAltar(Peasent peasent)
{
this.peasent = peasent;
}
#region ICommand 成员
public object Execute()
{
return peasent.BuildAltar();
}
#endregion
}
public class UnBuildAltar : ICommand
{
Peasent peasent;
public UnBuildAltar(Peasent peasent)
{
this.peasent = peasent;
}
#region ICommand 成员
public object Execute()
{
return peasent.UnBuildAltar();
}
#endregion
}
public class BuildBarracks : ICommand
{
Peasent peasent;
public BuildBarracks(Peasent peasent)
{
this.peasent = peasent;
}
#region ICommand 成员
public object Execute()
{
return peasent.BuildBarracks();
}
#endregion
}
public class UnBuildBarracks : ICommand
{
Peasent peasent;
public UnBuildBarracks(Peasent peasent)
{
this.peasent = peasent;
}
#region ICommand 成员
public object Execute()
{
return peasent.UnBuildBarracks();
}
#endregion
}
public class ProductSoldier : ICommand
{
Barracks barracks;
public ProductSoldier(Barracks barracks)
{
this.barracks = barracks;
}
#region ICommand 成员
public object Execute()
{
return barracks.ProductSoldier();
}
#endregion
}
public class UnProductSoldier : ICommand
{
Barracks barracks;
public UnProductSoldier(Barracks barracks)
{
this.barracks = barracks;
}
#region ICommand 成员
public object Execute()
{
return barracks.UnProductSoldier();
}
#endregion
}
public class Peasent
{
public Alltar BuildAltar()
{
LandpyForm.Form.OutputResult("BuildAltar");
return new Alltar();
}
public object UnBuildAltar()
{
LandpyForm.Form.OutputResult("Undo BuildAltar");
return null;
}
public object BuildBarracks()
{
LandpyForm.Form.OutputResult("BuildBarracks");
return new Barracks();
}
public object UnBuildBarracks()
{
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 class Soldier
{
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DotaCommon;
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)
{
return ((ICommand)commands[button]).Execute();
}
}
public interface ICommand
{
object Execute();
}
public class BuildAltar : ICommand
{
Peasent peasent;
public BuildAltar(Peasent peasent)
{
this.peasent = peasent;
}
#region ICommand 成员
public object Execute()
{
return peasent.BuildAltar();
}
#endregion
}
public class UnBuildAltar : ICommand
{
Peasent peasent;
public UnBuildAltar(Peasent peasent)
{
this.peasent = peasent;
}
#region ICommand 成员
public object Execute()
{
return peasent.UnBuildAltar();
}
#endregion
}
public class BuildBarracks : ICommand
{
Peasent peasent;
public BuildBarracks(Peasent peasent)
{
this.peasent = peasent;
}
#region ICommand 成员
public object Execute()
{
return peasent.BuildBarracks();
}
#endregion
}
public class UnBuildBarracks : ICommand
{
Peasent peasent;
public UnBuildBarracks(Peasent peasent)
{
this.peasent = peasent;
}
#region ICommand 成员
public object Execute()
{
return peasent.UnBuildBarracks();
}
#endregion
}
public class ProductSoldier : ICommand
{
Barracks barracks;
public ProductSoldier(Barracks barracks)
{
this.barracks = barracks;
}
#region ICommand 成员
public object Execute()
{
return barracks.ProductSoldier();
}
#endregion
}
public class UnProductSoldier : ICommand
{
Barracks barracks;
public UnProductSoldier(Barracks barracks)
{
this.barracks = barracks;
}
#region ICommand 成员
public object Execute()
{
return barracks.UnProductSoldier();
}
#endregion
}
public class Peasent
{
public Alltar BuildAltar()
{
LandpyForm.Form.OutputResult("BuildAltar");
return new Alltar();
}
public object UnBuildAltar()
{
LandpyForm.Form.OutputResult("Undo BuildAltar");
return null;
}
public object BuildBarracks()
{
LandpyForm.Form.OutputResult("BuildBarracks");
return new Barracks();
}
public object UnBuildBarracks()
{
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 class Soldier
{
}
}