static void Main(string[] args) { // Step1.模拟显示功能键设置窗口 FBSettingWindow window = new FBSettingWindow("功能键设置窗口"); // Step2.假如目前要设置两个功能键 FunctionButton buttonA = new FunctionButton("功能键A"); FunctionButton buttonB = new FunctionButton("功能键B"); // Step3.读取配置文件和反射生成具体命令对象 Command commandA = new HelpCommand(); Command commandB = new MinimizeCommand(); // Step4.将命令注入功能键 buttonA.SetCommand(commandA); buttonB.SetCommand(commandB); window.AddFunctionButton(buttonA); window.AddFunctionButton(buttonB); window.Display(); // Step5.调用功能键的业务方法 buttonA.OnClick(); buttonB.OnClick(); Console.ReadKey(); }
static void Main(string[] args) { Debug.WriteLine("Debug 命令模式"); FBSettingWindow fbsw = new FBSettingWindow("功能键设置"); FunctionButton fb1, fb2; fb1 = new FunctionButton("功能键1"); fb2 = new FunctionButton("功能键2"); Command command1, command2; //通过读取配置文件和反射生成具体命令对象 command1 = new HelpCommand(); command2 = new MinimizeCommand(); //将命令对象注入功能键 fb1.setCommand(command1); fb2.setCommand(command2); fbsw.addFunctionButton(fb1); fbsw.addFunctionButton(fb2); fbsw.display(); //调用功能键的业务方法 fb1.onClick(); fb2.onClick(); Console.WriteLine("》》-----------------------------"); FunctionButton2 fb3 = new FunctionButton2("功能设置3"); CommandQueue cmd = new CommandQueue(); cmd.addCommand(command1); cmd.addCommand(command2); fb3.setCommandQueue(cmd); fb3.onClick(); Console.WriteLine("-----------------------------"); CalculatorForm form = new CalculatorForm(); AbstractCommand command; command = new ConcreteCommand(); form.setCommand(command); //向发送者注入命令对象 form.compute(10); form.compute(5); form.compute(10); form.undo(); Console.ReadKey(); }