码迷,mamicode.com
首页 > 其他好文 > 详细

命令模式

时间:2016-08-28 16:25:58      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*


命令模式


*
* 可以对操作相关命令处理进行过滤和取消回退的操作
* (让命令和操作分开来)

*/

namespace App_MYCS.HDL_SJMS.MLMS
{
class my_MLMS
{
public void dy()
{
Receiver r = new Receiver();
Command c = new concreteCommand(r);
Invoker i = new Invoker();
i.SetCommand(c);
i.ExecuteCommand();
}
}

//执行操作接口
abstract class Command
{
protected Receiver receiver;

public Command(Receiver r)
{
this.receiver = r;
}

abstract public void Execute();
}

//把接收者对象绑定于一个动作
class concreteCommand : Command
{
public concreteCommand(Receiver r)
: base(r)
{
}

public override void Execute()
{
receiver.Action();
}
}

class Invoker
{
private Command command;

public void SetCommand(Command c)
{
this.command = c;
}
public void ExecuteCommand()
{
command.Execute();
}
}

//
class Receiver
{
public void Action()
{
Console.WriteLine("执行请求");
}
}

 


//-----------------------------------------------------------
//点菜小例

public class Barbecuer
{
//相应的操作

public void Runfunction1()
{
Console.WriteLine("执行操作1");
}

public void Runfunction2()
{
Console.WriteLine("执行操作2");
}
}

public abstract class commands
{
//抽象出
protected Barbecuer r;
public commands(Barbecuer r)
{
this.r = r;
}

abstract public void ExcuteCommand();
}

class bake1 : commands
{
public bake1(Barbecuer r)
: base(r)
{

}
public override void ExcuteCommand()
{
r.Runfunction1();
}
}

class bake2 : commands
{
public bake2(Barbecuer r)
: base(r)
{

}
public override void ExcuteCommand()
{
r.Runfunction2();
}
}

 


public class Waiter
{
private IList<commands> orders = new List<commands>();

//设置
public void Setord(commands c)
{
//增加条件进通知的 c 命令进行过滤
orders.Add(c);
}

//取消
public void CancelOrder(commands c)
{
//取消之前增加的命令
orders.Remove(c);
}

//通知全部执行
public void Notify()
{
foreach (commands c in orders)
{
c.ExcuteCommand();
}
}
}


class MLMS_dy
{
public void dy()
{
Barbecuer boy = new Barbecuer();//详细的操作

commands bakec1 = new bake1(boy);//加载命令
commands bakec2 = new bake2(boy);//

Waiter g = new Waiter();

g.Setord(bakec1);
g.Setord(bakec2);

g.Notify();

 


}
}

}

命令模式

标签:

原文地址:http://www.cnblogs.com/longkai178/p/5815111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!