Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1279762
  • 博文数量: 296
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 2986
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(296)

文章存档

2020年(10)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-02-20 15:02:13

这个command pattern 貌似用处还挺多的,而且也算是比教复杂的pattern 了,anyway ,直接上代码。 

一个简单的代码如下:

点击(此处)折叠或打开

  1. package CommandPattern;

  2. //Command interface with an execute() method

  3. interface Command{
  4.     public void execute();
  5. }

  6. //Lunch is a receiver
  7. class Lunch{
  8.     public void makeLunch(){
  9.         System.out.println("Lunch is being made");
  10.     }
  11. }

  12. //LunchCommand implements Command, it contains a reference to Lunch,a receiver
  13. // Its execute() method invokes the appropriate action on the receiver

  14. class LunchCommand implements Command{
  15.     Lunch lunch;


  16.     public LunchCommand(Lunch lunch){
  17.         this.lunch = lunch;
  18.     }

  19.     @Override
  20.     public void execute(){
  21.         lunch.makeLunch();
  22.     }
  23. }

  24. //Dinner is also receiver
  25. class Dinner{
  26.     public void makeDinner(){
  27.         System.out.println("Dinner is being made");
  28.     }
  29. }


  30. /* The DinnerCommand is similar to LunchCommand.
  31.  * it contains a reference to Dinner, a receiver.
  32.  * Its execute() method invokes the makeDinner() action of the Dinner object
  33.  */

  34. class DinnerCommand implements Command{
  35.     Dinner dinner;

  36.     public DinnerCommand(Dinner dinner){
  37.         this.dinner = dinner;
  38.     }

  39.     @Override
  40.     public void execute(){
  41.         dinner.makeDinner();
  42.     }
  43. }

  44. /* MealInvoker is the invoker class, it contains a reference to the Command
  45.  * to invoke . Its invoke() method calls the execute() method of the Command
  46.  */

  47. class MealInvoker{
  48.     Command command;

  49.     public MealInvoker(Command command){
  50.         this.command = command;
  51.     }

  52.     public void setCommand(Command command){
  53.         this.command = command;
  54.     }

  55.     public void invoke(){
  56.         command.execute();
  57.     }
  58. }


  59. public class CommandPatternDemo {
  60.     public static void main(String[] args){
  61.         Lunch lunch = new Lunch();    //receiver
  62.         Command lunchCommand = new LunchCommand(lunch);        //concrete command

  63.         Dinner dinner = new Dinner();
  64.         Command dinnerCommand = new DinnerCommand(dinner);        //concrete command

  65.         MealInvoker mi = new MealInvoker(lunchCommand);        //invoker
  66.         mi.invoke();

  67.         mi.setCommand(dinnerCommand);
  68.         mi.invoke();
  69.     }
  70. }

下面这个要稍微复杂些了
参考了这个代码: https://gist.github.com/cangelis/1442951#file-command-class-php-L36

点击(此处)折叠或打开

  1. package CommandPattern;

  2. interface MyCommand{
  3.     public void Execute();
  4.     public void unExecute();
  5. }

  6. class Calculator{
  7.     private int current;

  8.     public Calculator(int current){
  9.         this.current = current;
  10.     }

  11.     public void Action(String operator, int operand){
  12.         switch(operator){
  13.             case "+":
  14.                 this.current += operand;
  15.                 break;
  16.             case "-":
  17.                 this.current -= operand;
  18.                 break;
  19.             case "*":
  20.                 this.current *= operand;
  21.                 break;
  22.             case "/":
  23.                 this.current /= operand;
  24.                 break;
  25.         }
  26.     }

  27.     public int getCurrent(){
  28.         return this.current;
  29.     }
  30. }


  31. class concreteCommand implements MyCommand{
  32.     protected String operator ;
  33.     protected int operand;
  34.     protected int _lastnum;
  35.     Calculator cal;


  36.     public concreteCommand(String operator,int operand, Calculator cal,int _lastnum){
  37.         this.operand = operand;
  38.         this.operator = operator;
  39.         this.cal = cal;
  40.         this._lastnum = _lastnum;
  41.     }

  42.     public void Execute(){
  43.         cal.Action(operator, operand);
  44.     }

  45.     public void unExecute(){
  46.         cal.Action(Undo(operator), operand);
  47.     }

  48.     private String Undo(String operator){
  49.         switch(operator){
  50.             case "+": return "-" ;
  51.             case "-": return "+" ;
  52.             case "*": return "/" ;
  53.             case "/": return "*" ;
  54.             default:
  55.                 System.out.println("wrong operand");
  56.                 return null;
  57.         }
  58.     }
  59. }

  60. class CalInvoker {
  61.     MyCommand command;

  62.     public CalInvoker(MyCommand command){
  63.         this.command = command;
  64.     }

  65.     public void setInvoker(MyCommand command){
  66.         this.command = command;
  67.     }

  68.     public void Invoke(){
  69.         command.Execute();
  70.     }

  71.     public void unInvoke(){
  72.         command.unExecute();
  73.     }
  74. }

  75. public class CommandPatternDemo2 {
  76.     public static void main(String[] args){


  77.         Calculator ca = new Calculator(0);
  78.         MyCommand cm1 = new concreteCommand("+",5,ca,0);

  79.         CalInvoker ci = new CalInvoker(cm1);
  80.         ci.Invoke();
  81.         System.out.println(ca.getCurrent());
  82.         ci.unInvoke();
  83.         System.out.println(ca.getCurrent());
  84.     }
  85. }


阅读(458) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~