


1 2 3 4 5 6 7 8 9 10 11 | public interface ICommand { /// <summary> /// 执行命令 /// </summary> string Execute(); /// <summary> /// 撤消命令 /// </summary> string UnExecute(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class SystemCommand { public string StartUp() { return "手机开机" ; } public string ShutDown() { return "手机关机" ; } public string SendSMS() { return "发短信" ; } public string RemoveSMS() { return "删短信" ; } public string CallPhone() { return "打电话" ; } public string RingOff() { return "挂电话" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public abstract class MobileSystem :ICommand { private SystemCommand mobileCommand; public SystemCommand MobileCommand { get { return mobileCommand; } set { mobileCommand = value; } } public MobileSystem(SystemCommand command) { this .mobileCommand = command; } #region ICommand 成员 public abstract string Execute(); public abstract string UnExecute(); #endregion } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class StartUp :MobileSystem { #region MobileSystem 成员 public override string Execute() { return MobileCommand.StartUp(); } public override string UnExecute() { return MobileCommand.ShutDown(); } #endregion public StartUp(SystemCommand command) : base (command) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class Call : MobileSystem { #region MobileSystem 成员 public override string Execute() { return MobileCommand.CallPhone(); } public override string UnExecute() { return MobileCommand.RingOff(); } #endregion public Call(SystemCommand command) : base (command) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class SMS : MobileSystem { #region MobileSystem 成员 public override string Execute() { return MobileCommand.SendSMS(); } public override string UnExecute() { return MobileCommand.RemoveSMS(); } #endregion public SMS(SystemCommand command) : base (command) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class MobileServer : ICommand { private MobileSystem mobileSystem; public MobileServer(MobileSystem system) { this .mobileSystem = system; } public string Execute() { return mobileSystem.Execute(); } public string UnExecute() { return mobileSystem.UnExecute(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public partial class Run : Form { public Run() { InitializeComponent(); } private void btnRun_Click( object sender, EventArgs e) { SystemCommand command = new SystemCommand(); MobileServer server = new MobileServer( new StartUp(command)); rtbResult.AppendText(server.Execute() + "\n" ); rtbResult.AppendText(server.UnExecute() + "\n" ); server = new MobileServer( new Call(command)); rtbResult.AppendText(server.Execute() + "\n" ); rtbResult.AppendText(server.UnExecute() + "\n" ); server = new MobileServer( new SMS(command)); rtbResult.AppendText(server.Execute() + "\n" ); rtbResult.AppendText(server.UnExecute() + "\n" ); } } |