23命令模式Command

一、什么是命令模式

  Command模式也叫命令模式 ,是行为设计模 式的一种。Command模式通过被称为 Command的类封装了对目标对象的调用行为以及调用参数。

二、命令模式的应用场景

  在面向对象的程序设计中,一个对象调用另一个对象, 一般情况下的调用过程是:创建目标对象实例;设置调 用参数;调用目标对象的方法。

  但在有些情况下有必要使用一个专门的类对这种调用 过程加以封装,我们把这种专门的类称作command类。

     - 整个调用过程比较繁杂,或者存在多处这种调用。 这时,使用Command类对该调用加以封装,便于功能的 再利用。

     - 调用前后需要对调用参数进行某些处理。

    - 调用前后需要进行某些额外处理,比如日志,缓存,记录历史操作等。

三、命令模式的结构

四、命令模式的角色和职责

  Command     Command抽象类。

  ConcreteCommand     Command的具体实现类。

  Receiver     需要被调用的目标对象。

  Invorker     通过Invorker执行Command对象。

ex1:

小商贩

 1 /*
 2  * 小商贩
 3  */
 4 public class Peddler {
 5     
 6     //卖苹果
 7     public void sailApple() {
 8         System.out.println("卖苹果");
 9     }
10     
11     //卖香蕉
12     public void sailBanana() {
13         System.out.println("卖香蕉");
14     }
15 }

测试

1 public class MainClass {
2     public static void main(String[] args) {
3         Peddler peddler = new Peddler();
4         peddler.sailApple();
5         peddler.sailBanana();
6     }
7 }

================================================================================

ex2:

小商贩

 1 /*
 2  * 小商贩
 3  */
 4 public class Peddler {
 5     
 6     //卖苹果
 7     public void sailApple() {
 8         System.out.println("卖苹果");
 9     }
10     
11     //卖香蕉
12     public void sailBanana() {
13         System.out.println("卖香蕉");
14     }
15 }

命令

 1 //命令
 2 public abstract class Command {
 3     private Peddler peddler;
 4     
 5     
 6     public Command(Peddler peddler) {
 7         this.peddler = peddler;
 8     }
 9     
10     public Peddler getPeddler() {
11         return peddler;
12     }
13 
14     public void setPeddler(Peddler peddler) {
15         this.peddler = peddler;
16     }
17 
18     public abstract void sail();
19 }

苹果命令

 1 //苹果命令
 2 public class AppleCommand extends Command {
 3 
 4     public AppleCommand(Peddler peddler) {
 5         super(peddler);
 6     }
 7 
 8     public void sail() {
 9         this.getPeddler().sailApple();
10     }
11 }

香蕉命令

 1 //香蕉命令
 2 public class BananaCommand extends Command{
 3 
 4     public BananaCommand(Peddler peddler) {
 5         super(peddler);
 6     }
 7 
 8     public void sail() {
 9         this.getPeddler().sailBanana();
10     }
11 }

测试

 1 public class MainClass {
 2     public static void main(String[] args) {
 3         Peddler peddler = new Peddler();
 4 //        peddler.sailApple();
 5 //        peddler.sailBanana();
 6         
 7         Command appleCommand = new AppleCommand(peddler);
 8         Command bananaCommand = new BananaCommand(peddler);
 9         appleCommand.sail();
10         bananaCommand.sail();
11     }
12 }

=====================================================

ex3:

小商贩

 1 /*
 2  * 小商贩
 3  */
 4 public class Peddler {
 5     
 6     //卖苹果
 7     public void sailApple() {
 8         System.out.println("卖苹果");
 9     }
10     
11     //卖香蕉
12     public void sailBanana() {
13         System.out.println("卖香蕉");
14     }
15 }

命令  抽象

 1 //命令
 2 public abstract class Command {
 3     private Peddler peddler;
 4     
 5     
 6     public Command(Peddler peddler) {
 7         this.peddler = peddler;
 8     }
 9     
10     public Peddler getPeddler() {
11         return peddler;
12     }
13 
14     public void setPeddler(Peddler peddler) {
15         this.peddler = peddler;
16     }
17 
18     public abstract void sail();
19 }

苹果命令

 1 //苹果命令
 2 public class AppleCommand extends Command {
 3 
 4     public AppleCommand(Peddler peddler) {
 5         super(peddler);
 6     }
 7 
 8     public void sail() {
 9         this.getPeddler().sailApple();
10     }
11 }

香蕉命令

 1 package com.ibeifemg.ex3;
 2 //香蕉命令
 3 public class AppleCommand extends Command {
 4 
 5     public AppleCommand(Peddler peddler) {
 6         super(peddler);
 7     }
 8 
 9     public void sail() {
10         this.getPeddler().sailApple();
11     }
12 }

服务

 1 //服务
 2 public class Waiter {
 3     private Command command;
 4 
 5     public Command getCommand() {
 6         return command;
 7     }
 8 
 9     public void setCommand(Command command) {
10         this.command = command;
11     }
12 
13     public void sail() {
14         command.sail();
15     }
16 }

测试

 1 public class MainClass {
 2     public static void main(String[] args) {
 3         Peddler peddler = new Peddler();
 4 //        peddler.sailApple();
 5 //        peddler.sailBanana();
 6         
 7         Command appleCommand = new AppleCommand(peddler);
 8         Command bananaCommand = new BananaCommand(peddler);
 9 //        appleCommand.sail();
10 //        bananaCommand.sail();
11         Waiter waiter = new Waiter();
12         waiter.setCommand(appleCommand);
13         waiter.sail();
14         waiter.setCommand(bananaCommand);
15         waiter.sail();
16     }
17 }

========================================================================

ex4:

小商贩

 1 /*
 2  * 小商贩
 3  */
 4 public class Peddler {
 5     
 6     //卖苹果
 7     public void sailApple() {
 8         System.out.println("卖苹果");
 9     }
10     
11     //卖香蕉
12     public void sailBanana() {
13         System.out.println("卖香蕉");
14     }    
15 }

命令

 1 //命令
 2 public abstract class Command {
 3     private Peddler peddler;
 4     
 5     
 6     public Command(Peddler peddler) {
 7         this.peddler = peddler;
 8     }
 9     
10     public Peddler getPeddler() {
11         return peddler;
12     }
13 
14     public void setPeddler(Peddler peddler) {
15         this.peddler = peddler;
16     }
17 
18     public abstract void sail();
19 }

苹果命令

 1 //苹果命令
 2 public class AppleCommand extends Command {
 3 
 4     public AppleCommand(Peddler peddler) {
 5         super(peddler);
 6     }
 7 
 8     public void sail() {
 9         this.getPeddler().sailApple();
10     }
11 }

香蕉命令

 1 //香蕉命令
 2 public class BananaCommand extends Command{
 3 
 4     public BananaCommand(Peddler peddler) {
 5         super(peddler);
 6     }
 7 
 8     public void sail() {
 9         this.getPeddler().sailBanana();
10     }
11 }

服务

 1 //服务
 2 public class Waiter {
 3     private List<Command> commands = new ArrayList<Command>();
 4 
 5 
 6     public void setOrder(Command command) {
 7         commands.add(command);
 8     }
 9 
10     public void removeOrder(Command command) {
11         commands.remove(command);
12     }
13     
14     public void sail() {
15         for(Command command : commands) {
16             command.sail();
17         }
18     }
19 }

测试

 1 public class MainClass {
 2     public static void main(String[] args) {
 3         Peddler peddler = new Peddler();
 4 //        peddler.sailApple();
 5 //        peddler.sailBanana();
 6         
 7         Command appleCommand = new AppleCommand(peddler);
 8         Command bananaCommand = new BananaCommand(peddler);
 9 //        appleCommand.sail();
10 //        bananaCommand.sail();
11         Waiter waiter = new Waiter();
12         
13         //下订单
14         waiter.setOrder(appleCommand);
15         waiter.setOrder(bananaCommand);
16         
17         //移除订单某项
18         waiter.removeOrder(appleCommand);
19         
20         waiter.sail();
21     }
22 }

猜你喜欢

转载自www.cnblogs.com/justdoitba/p/9035389.html