您的位置:首页 > 编程语言 > C#

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);

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: