【设计模式】14、命令模式

【设计模式】14、命令模式_第1张图片

 

 

 1 package com.shejimoshi.behavioral.Command;
 2 
 3 
 4 /**
 5  * 功能:接收命令着者,这个人可以执行多种命令
 6  * 时间:2016年3月2日上午11:07:30
 7  * 作者:cutter_point
 8  */
 9 public class Recipient
10 {
11     public void findPhoneNum()
12     {
13         System.out.println("查找手机号码");
14     }
15     
16     public void findPhoneSite()
17     {
18         System.out.println("查找手机位置");
19     }
20 }

 

 1 package com.shejimoshi.behavioral.Command;
 2 
 3 
 4 /**
 5  * 功能:命令抽象类
 6  * 时间:2016年3月2日上午11:06:32
 7  * 作者:cutter_point
 8  */
 9 public abstract class Command
10 {
11     //命令的受理者
12     protected Recipient rec;
13     
14     public Command(Recipient r)
15     {
16         this.rec = r;
17     }
18     
19     //执行命令
20     public abstract void execute();
21 }

 

 1 package com.shejimoshi.behavioral.Command;
 2 
 3 
 4 /**
 5  * 功能:查找手机号码
 6  * 时间:2016年3月2日上午11:14:11
 7  * 作者:cutter_point
 8  */
 9 public class FindPhoneNumCommand extends Command
10 {
11 
12     public FindPhoneNumCommand(Recipient r)
13     {
14         super(r);
15     }
16 
17     @Override
18     public void execute()
19     {
20         this.rec.findPhoneNum();
21     }
22 
23 }

 

 1 package com.shejimoshi.behavioral.Command;
 2 
 3 
 4 /**
 5  * 功能:查找手机位置
 6  * 时间:2016年3月2日上午11:11:58
 7  * 作者:cutter_point
 8  */
 9 public class FindPhoneSiteCommand extends Command
10 {
11     
12     public FindPhoneSiteCommand(Recipient r)
13     {
14         super(r);
15     }
16     
17     @Override
18     public void execute()
19     {
20         //进行查找
21         this.rec.findPhoneSite();
22     }
23 
24 }

 

 1 package com.shejimoshi.behavioral.Command;
 2 
 3 import java.text.DateFormat;
 4 import java.text.SimpleDateFormat;
 5 import java.util.ArrayList;
 6 import java.util.Date;
 7 import java.util.List;
 8 
 9 
10 /**
11  * 功能:命令发起者
12  * 时间:2016年3月2日上午11:15:16
13  * 作者:cutter_point
14  */
15 public class Sponsor
16 {
17     private List<Command> orders = new ArrayList<Command>();
18     
19     //设置订单
20     public void setOrder(Command command)
21     {
22         orders.add(command);
23         //获得时间
24         Date data = new Date();
25         DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
26         System.out.println("增加订单" + command.getClass().getName() + "\t时间:" + format.format(data));
27     }
28     
29     //取消订单
30     public void cancelOrder(Command command)
31     {
32         orders.remove(command);
33         //获得时间
34         Date data = new Date();
35         DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
36         System.out.println("取消订单" + command.getClass().getName() + "\t时间:" + format.format(data));
37     }
38     
39     //执行全部订单
40     public void notigy()
41     {
42         for(Command cmd : orders)
43         {
44             cmd.execute();
45         }
46     }
47 }

 

 1 package com.shejimoshi.behavioral.Command;
 2 
 3 
 4 /**
 5  * 功能:将一个请求封装为一个对象,从而使你可以用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作
 6  * 时间:2016年3月2日上午11:04:34
 7  * 作者:cutter_point
 8  */
 9 public class Test
10 {
11     public static void main(String[] args)
12     {
13         //首先店员准备
14         Recipient rec = new Recipient();
15         //那些服务命令
16         Command cmd1 = new FindPhoneNumCommand(rec);
17         Command cmd2 = new FindPhoneSiteCommand(rec);
18         
19         //下单者准备
20         Sponsor sps = new Sponsor();
21         
22         //开门迎客
23         sps.setOrder(cmd1);
24         sps.setOrder(cmd2);
25         
26         //店面开启运行模式
27         sps.notigy();
28         
29     }
30 }

 

测试结果:

增加订单com.shejimoshi.behavioral.Command.FindPhoneNumCommand	时间:2016-03-02
增加订单com.shejimoshi.behavioral.Command.FindPhoneSiteCommand	时间:2016-03-02
查找手机号码
查找手机位置

  

 

你可能感兴趣的:(【设计模式】14、命令模式)