zoukankan      html  css  js  c++  java
  • command 模式

    命令模式是对命令的封装。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。

    命令模式第一版:


    public class vendor {

    public void bake_mutton(int number) {

    DebugLog.log("i am baking mutton");

    }

    public void bake_pork(int number) {

    DebugLog.log("i am baking pork");

    }

    public void bake_beef(int number) {

    DebugLog.log("i am baking  beef");

    }

    }

     
     
    public class Client {
     
    /**
    * @param args
    */
    public static void main(String[] args) {
    vendor aVendor=new vendor();
    aVendor.bake_beef(3);
    aVendor.bake_mutton(5);
    aVendor.bake_pork(5);
     
    }
     
    }
    问题很明显,如果很多客户需要吃烧烤,做烧烤的小贩
    是不是很容易忙中出错,如果加入一个waiter,由waiter
    直接和vendor和client打交道,是不是出错的几率会小很多。
     
    public class BakeBeefCommand extends Command {
     
    public BakeBeefCommand(vendor receiver) {
    super(receiver);
    // TODO Auto-generated constructor stub
    }
     
    @Override
    public void command(int number) {
    this.receiver.bake_beef(number);
     
    }
     
    }
     
    public class BakeMuttonCommand extends Command {
     
    public BakeMuttonCommand(vendor receiver) {
    super(receiver);
    // TODO Auto-generated constructor stub
    }
     
    @Override
    public void command(int number) {
    this.receiver.bake_mutton(number);
     
    }
     
    }
     
    public class BakePorkCommand extends Command {
     
    public BakePorkCommand(vendor receiver) {
    super(receiver);
    // TODO Auto-generated constructor stub
    }
     
    @Override
    public void command(int number) {
    this.receiver.bake_pork(number);
     
    }
     
    }
     
    public abstract class Command {
    protected vendor receiver;
     
    public Command(vendor receiver) {
    super();
    this.receiver = receiver;
    }
     
    public abstract void command(int number);
    }
     
     
    public class vendor {
    public void bake_mutton(int number) {
    DebugLog.log("i am baking mutton");
    }
     
    public void bake_pork(int number) {
    DebugLog.log("i am baking pork");
    }
     
    public void bake_beef(int number) {
    DebugLog.log("i am baking  beef");
    }
    }
     
     
    public class Waiter {
    public void notify(Command command,int number) {
    command.command(number);
    }
    }
     
     
    public class Client {
     
    /**
    * @param args
    */
    public static void main(String[] args) {
    vendor aVendor=new vendor();
    Command beefCommand=new BakeBeefCommand(aVendor);
    Command muttonCommand=new BakeMuttonCommand(aVendor);
    Command porkCommand=new BakePorkCommand(aVendor);
    Waiter aWaiter=new Waiter();
    aWaiter.notify(beefCommand, 3);
    aWaiter.notify(muttonCommand, 3);
    aWaiter.notify(porkCommand, 3);
    }
     
    }
     
    由test代码可以看出,为什么一个request需要三次通知vendor啊,如果在烧烤
    过程中,client说我吃饱了,点的羊肉不要了怎么办?重构如下:
     
    public class BakeBeefCommand extends Command {
     
     
    public BakeBeefCommand(vendor receiver, int number) {
    super(receiver, number);
    // TODO Auto-generated constructor stub
    }
     
    @Override
    public void command() {
    this.receiver.bake_beef(this.number);
     
    }
     
     
     
    }
     
     
    public class BakeMuttonCommand extends Command {
     
     
    public BakeMuttonCommand(vendor receiver, int number) {
    super(receiver, number);
    }
     
    @Override
    public void command() {
    this.receiver.bake_mutton(this.number);
    }
     
    }
     
     
     
    public class BakePorkCommand extends Command {
     
     
    public BakePorkCommand(vendor receiver, int number) {
    super(receiver, number);
    }
     
    @Override
    public void command() {
    this.receiver.bake_pork(this.number);
     
    }
     
     
    }
     
    public abstract class Command {
    protected vendor receiver;
        protected int number;
        protected boolean isCancled = false;
    public boolean isCancled() {
    return isCancled;
    }
     
    public void setCancled(boolean isCancled) {
    this.isCancled = isCancled;
    }
     
    public Command(vendor receiver, int number) {
    super();
    this.receiver = receiver;
    this.number = number;
    }
     
    public abstract void command();
     
    public void execute()
    {
    if(isCancled)
    DebugLog.log("command has cancled");
    else
    command();
    }
     
    }
     
     
    public class vendor {
    public void bake_mutton(int number) {
    DebugLog.log("i am baking mutton");
    }
     
    public void bake_pork(int number) {
    DebugLog.log("i am baking pork");
    }
     
    public void bake_beef(int number) {
    DebugLog.log("i am baking  beef");
    }
    }
     
     
     
    public class Waiter {
    private List<Command> commands = new ArrayList<Command>();
     
    public void attach(Command command) {
    commands.add(command);
    }
     
    public void detach(Command command) {
    commands.remove(command);
    }
     
    public void Notify() {
    int size = commands.size();
    for (int index = 0; index < size; index++) {
    Command aCommand = commands.get(index);
    aCommand.execute();
    }
    }
    }
     
     
     
    public class Client {
     
    /**
    * @param args
    */
    public static void main(String[] args) {
    vendor aVendor=new vendor();
    Command beefCommand=new BakeBeefCommand(aVendor,3);
    Command muttonCommand=new BakeMuttonCommand(aVendor,5);
    Command porkCommand=new BakePorkCommand(aVendor,6);
    porkCommand.setCancled(true);
    Waiter aWaiter=new Waiter();
    aWaiter.attach(beefCommand);
    aWaiter.attach(muttonCommand);
    aWaiter.attach(porkCommand);
    aWaiter.Notify();
    porkCommand.setCancled(false);
    aWaiter.Notify();
    }
     
    }
     
  • 相关阅读:
    程序性能优化1
    在archlinux上搭建twitter storm cluster
    .Net 跨平台可移植类库PCL可用于任何平台包括Mono
    .NET面向对象特性之封装
    哈夫曼(Huffman)编码
    高质量视频学习网站
    (Java实现) 洛谷 P1042 乒乓球
    (Java实现) 洛谷 P1042 乒乓球
    (Java实现) 洛谷 P1071 潜伏者
    (Java实现) 洛谷 P1071 潜伏者
  • 原文地址:https://www.cnblogs.com/budoudou/p/2302032.html
Copyright © 2011-2022 走看看