C#命令模式
2015-10-19 17:20
651 查看
C#命令模式
C#命令模式可以将一个请求或者操作封装到一个对象中Command.cs 命令接口类 : 定义了一个excute接口 所有命令都要继承并实现这个接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CommandPatterns { //命令接口 public interface Command { void excute(); } }
StereoOnCommand.cs 开音响命令
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CommandPatterns { /// <summary> /// 音响打开命令 /// </summary> public class StereoOnCommand : Command { Stereo stereo; // 构造方法将音响类的对象传递过来 保留其引用 为了在excute方法中调用改引用的一些公有函数 public StereoOnCommand(Stereo stereo) { this.stereo = stereo; } public void excute() { //这里表示On 按钮的 音响行为 stereo.startup(); stereo.setValume(11); stereo.playMusic(); } } }
StereoOffCommand.cs 关音响命令
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CommandPatterns { /// <summary> /// 音响关闭命令 /// </summary> public class StereoOffCommand : Command { Stereo stereo; // 构造方法将音响类的对象传递过来 保留其引用 为了在excute方法中调用改引用的一些公有函数 public StereoOffCommand(Stereo stereo) { this.stereo = stereo; } // 继承自Command接口 必须实现excute public void excute() { //这里表示Off 按钮的 音响行为 stereo.stopMusic(); stereo.shutdown(); } } }
Stereo.cs音响类 (命令的执行者)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CommandPatterns { /// <summary> /// 音响类 /// </summary> public class Stereo { //设置声音 public void setValume(int valume) { Console.WriteLine(" Stereo set valume 11"); } //启动 public void startup() { Console.WriteLine(" Stereo is starting up"); } //关闭 public void shutdown() { Console.WriteLine(" Stereo is shuting down "); } public void stopMusic() { Console.WriteLine(" Stereo is stoping Music "); } public void playMusic() { Console.WriteLine(" Stereo is playing Music "); } } }
RemoteControl.cs 遥控器类 (命令发出者)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CommandPatterns { //遥控器类 遥控器有n组 on off 键 //每组on off 键可以实现每个产品的不同功能 public class RemoteControl { Command[] onCommands; Command[] offCommands; public RemoteControl(int pruductNumber) { onCommands = new Command[pruductNumber]; offCommands = new Command[pruductNumber]; } public void setCommand(int index , Command onCmd , Command offCmd) { onCommands[index] = onCmd; offCommands[index] = offCmd; } public void onOnButtonClicked(int index) { onCommands[index].excute(); } public void onOffButtonClicked(int index) { offCommands[index].excute(); } } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CommandPatterns { class Program { static void Main(string[] args) { RemoteControl myRemoteControl = new RemoteControl(7); //初始化产品 Stereo myStereo = new Stereo( ); StereoOnCommand stereoOnCmd = new StereoOnCommand(myStereo); StereoOffCommand stereoOffCmd = new StereoOffCommand(myStereo); //绑定产品与遥控器按钮的位置 myRemoteControl.setCommand(0, stereoOnCmd, stereoOffCmd); //模拟点击遥控器第index排 的on off 按钮 myRemoteControl.onOnButtonClicked(0); myRemoteControl.onOffButtonClicked(0); } } }
相关文章推荐
- AXIS2客户端代码连接C#web service带ntlm认证
- C# Excel检验数据是否重复及Table检验数据是否重复
- c#中异步编程
- 在C#中创建和读取XML文件
- 字符串公式解析器——使用“逆波兰式算法”及C#实现
- C#接口之IEnumerable,IEnumerator
- C#项目实战-实现一个多线程端口扫描器
- 【工作记录0014】C#"该行已经属于另一个表"解决方法
- C# Winform 界面中各控件随着窗口大小变化
- 修饰模式(Decorator结构化)C#简单的例子
- C#中 ListView控件行高的巧妙设置
- C#语法中一个问号(?)与两个问号(??)有什么区别
- C#
- C#中使用ListView动态添加数据不闪烁
- C#文件写入
- C#时间戳
- C#委托的介绍(delegate、Action、Func、predicate)
- C#学习日记26---接口(interface)类型
- 使用C#和Thrift来访问Hbase实例
- C#基础巩固(2)-Linq To XML创建XML