23命令模式Command

1、什麼是命令模式緩存

  Command模式也叫命令模式 ,是行爲設計模 式的一種。Command模式經過被稱爲 Command的類封裝了對目標對象的調用行爲以及調用參數。app

 

2、命令模式的應用場景測試

  在面向對象的程序設計中,一個對象調用另外一個對象, 通常狀況下的調用過程是:建立目標對象實例;設置調 用參數;調用目標對象的方法。 this

  但在有些狀況下有必要使用一個專門的類對這種調用 過程加以封裝,咱們把這種專門的類稱做command類。spa

     - 整個調用過程比較繁雜,或者存在多處這種調用。 這時,使用Command類對該調用加以封裝,便於功能的 再利用。設計

     - 調用先後須要對調用參數進行某些處理。 日誌

    - 調用先後須要進行某些額外處理,好比日誌,緩存,記錄歷史操做等。code

 

3、命令模式的結構對象

 

4、命令模式的角色和職責blog

  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 }
相關文章
相關標籤/搜索