1、请求者
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Command
{
/// <summary>
/// 接收者
/// </summary>
public class ConditionReceiver
{
public void On()
{
Console.WriteLine("空调开启");
}
public void Off()
{
Console.WriteLine("空调关闭");
}
public void Cool()
{
Console.WriteLine("空调制冷");
}
public void Warm()
{
Console.WriteLine("空调制热");
}
}
}
2、命令接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Command
{
/// <summary>
/// 命令接口
/// </summary>
public interface ICommand
{
/// <summary>
/// 执行命令
/// </summary>
void Execute();
}
}
3、具体命令
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Command
{
public class OnCommand : ICommand
{
/// <summary>
/// 对哪个 接受者 处理命令
/// </summary>
private ConditionReceiver receiver;
public OnCommand(ConditionReceiver receiver)
{
this.receiver = receiver;
}
/// <summary>
/// 具体接受者 执行命令
/// </summary>
public void Execute()
{
Console.WriteLine("OnCommand --> Execute");
receiver.On();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Command
{
public class OffCommand : ICommand
{
/// <summary>
/// 对哪个 接受者 处理命令
/// </summary>
private ConditionReceiver receiver;
public OffCommand(ConditionReceiver receiver)
{
this.receiver = receiver;
}
/// <summary>
/// 具体接受者 执行命令
/// </summary>
public void Execute()
{
Console.WriteLine("OffCommand --> Execute");
receiver.Off();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Command
{
public class CoolCommand : ICommand
{
/// <summary>
/// 对哪个 接受者 处理命令
/// </summary>
private ConditionReceiver receiver;
public CoolCommand(ConditionReceiver receiver)
{
this.receiver = receiver;
}
/// <summary>
/// 具体接受者 执行命令
/// </summary>
public void Execute()
{
Console.WriteLine("CoolCommand --> Execute");
receiver.Cool();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Command
{
public class WarmCommand : ICommand
{
/// <summary>
/// 对哪个 接受者 处理命令
/// </summary>
private ConditionReceiver receiver;
public WarmCommand(ConditionReceiver receiver)
{
this.receiver = receiver;
}
/// <summary>
/// 具体接受者 执行命令
/// </summary>
public void Execute()
{
Console.WriteLine("WarmCommand --> Execute");
receiver.Warm();
}
}
}
4、请求者
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Command
{
/// <summary>
/// 请求者
/// </summary>
public class AppInvoker
{
private ICommand onCommand;
private ICommand offCommand;
private ICommand coolCommand;
private ICommand warmCommand;
public void SetOnCommand(ICommand command)
{
this.onCommand = command;
}
public void SetOffCommand(ICommand command)
{
this.offCommand = command;
}
public void SetCoolCommand(ICommand command)
{
this.coolCommand = command;
}
public void SetWarmCommand(ICommand command)
{
this.warmCommand = command;
}
public void On()
{
this.onCommand.Execute();
}
public void Off()
{
this.offCommand.Execute();
}
public void Cool()
{
this.coolCommand.Execute();
}
public void warm()
{
this.warmCommand.Execute();
}
}
}
5、使用命令模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Command
{
class Program
{
static void Main(string[] args)
{
// 创建接受者
ConditionReceiver receiver = new ConditionReceiver();
// 创建命令对象 并设置接受者
ICommand onCommand = new OnCommand(receiver);
ICommand offCommand = new OffCommand(receiver);
ICommand coolCommand = new CoolCommand(receiver);
ICommand warmCommand = new WarmCommand(receiver);
// 创建请求者 设置命令对象
AppInvoker appInvoker = new AppInvoker();
appInvoker.SetOnCommand(onCommand);
appInvoker.SetOffCommand(offCommand);
appInvoker.SetCoolCommand(coolCommand);
appInvoker.SetWarmCommand(warmCommand);
// 执行请求命令
appInvoker.On();
Console.WriteLine("--------------");
appInvoker.Cool();
Console.WriteLine("--------------");
appInvoker.warm();
Console.WriteLine("--------------");
appInvoker.Off();
Console.WriteLine("--------------");
Console.ReadLine();
}
}
}
6、优缺点
优点:
1、调用者与接受者没有任何依赖、降低了系统的耦合性
2、扩展性比较强、新的命令可以很容易增加
缺点:
1、过多的命令会导致过多的具体命令类。