命令模式

  1. /*
  2.  * 关于命令模式的个人理解,不是很深透,先写篇短文记录一下学习历程,这几天陆续重写早段时间看的其他几个模式
  3.  * 命令模式
  4.  * 主要有几个角色:client,接受者,抽象命令接口,具体命令实现,调用者
  5.  * 下面写了个例子:
  6.  * Light 抽象灯类-接受者
  7.  * RoomLight,KitchenLight 具体类
  8.  * 
  9.  * Command 抽象命令接口
  10.  * LightOnCommand ....具体实现命令接口
  11.  * 
  12.  * CommandStream 个人感觉比较类似于批处理一样的东西,可以任意添加删除命令,最后按一个键RunCmd就可以实现
  13.  * 整串命令实行,各自调用各自的execute方法,具体如何实现还看各具体命令自定义的方法
  14.  * 
  15.  * Invoker 调用者,用于调用命令,setcommand触发当时要执行的命令
  16.  * undo操作等都在代码中描述
  17.  * 
  18.  * 对于命令模式,经过一点思考,总是想:如果不用命令模式,代码会变成怎么样》???
  19.  * 这个我觉得是关键问题,如果知道为什么要用命令模式,就会在以后可以灵活用到此东西
  20.  * 所以在查了一些资料和自己想了一些代码之后(后面注释的就是我想不用此模式写的代码)
  21.  * 可以看到:
  22.  * RoomLight1内部有on off等方法,如果要调用,还是可以就是new一个roomlight1出来,然后调用on之类东西
  23.  * 但是要实现上面的类似于CommandStream,还真的不知如何实现,如果在一个类中的一串命令,还好说,直接就on-off-turnto(2)-...
  24.  * 但是如果多起来,而且经常变动的话,也不好做!!
  25.  * 而因为在不同类中,如misic,roomlight1之间不同的命令,如何来区分不同的实例??(这是一个问题),
  26.  * 如果类中的方法在后来会扩展,变多的话,想了一下,改动会很复杂
  27.  * 另外,做undo等操作时,如何做??这也是比较麻烦的事!!
  28.  * 
  29.  * 所以,按照一些文章所说的:命令模式就将命令提升到类的层次,这样可以应对各种变化,调用者不用知道如何实现,只知道我要调用就ok
  30.  * 实现的也不用管谁来调用我,我只管我自己实现就行了!!
  31.  * 
  32.  * 上面是一些个人看法,理解不深!!
  33.  * 以后用到会加深理解
  34.  * 待续。。。。
  35.  * 
  36.  */
  37. package command_pack;
  38. import java.util.*;
  39. abstract class Light{
  40.     
  41.     private String lightname;
  42.     private int level;
  43.     
  44.     public Light(String str){
  45.         lightname = str;
  46.         level = 0;
  47.     }
  48.     
  49.     public void turnTo(int s){
  50.         level = s;
  51.     }
  52.     
  53.     public int getLevel(){
  54.         return level;
  55.     }
  56.     
  57.     public String getName(){
  58.         return lightname;
  59.     }
  60.     
  61.     public void print(){
  62.         
  63.         System.out.println(this.getName()+" :light level " + this.getLevel());
  64.         
  65.     }
  66. }
  67. class RoomLight extends Light{
  68.     public RoomLight(){
  69.         super("roomLight");
  70.     }
  71. }
  72. class KitchenLight extends Light{
  73.     public KitchenLight(){
  74.         super("kitchenLight");
  75.     }
  76. }
  77. class LightOnCommand implements Command{        
  78.     
  79.     private Light light;
  80.     
  81.     public LightOnCommand(Light l){             //带参数命令模式
  82.         light = l;
  83.     }
  84.     public void execute(){
  85.         light.turnTo(10);       //10 is the max level for simulation
  86.         light.print();
  87.         
  88.     }
  89. }
  90. class LightTurnCommand implements Command{
  91.     
  92.     private Light light;
  93.     private int level;
  94.     
  95.     public LightTurnCommand(Light li,int le){
  96.         light = li;
  97.         level = le;
  98.     }
  99.     public void execute(){
  100.         light.turnTo(level);
  101.         light.print();
  102.     }
  103.     
  104. }
  105. class LightOffCommand implements Command{
  106.     
  107.     private Light light;
  108.     public LightOffCommand(Light l){
  109.         light = l;
  110.     }
  111.     public void execute(){
  112.         light.turnTo(0);
  113.         light.print();
  114.     }
  115. }
  116. class MusicOnCommand implements Command{        //不带参数命令模式
  117.     
  118.     public void execute(){
  119.         System.out.println("Music on ");
  120.     }
  121.     
  122. }
  123. class MusicOffCommand implements Command{
  124.     public void execute(){
  125.         System.out.println("Music off ");
  126.     }
  127. }
  128. class CommandStream{        //命令流
  129.     
  130.     private Vector vec;
  131.     public CommandStream(){
  132.         vec = new Vector();
  133.     }
  134.     
  135.     public void addCommand(Command cmd){
  136.         vec.add(cmd);
  137.     }
  138.     
  139.     public void removeCommand(){
  140.         
  141.         vec.remove(vec.size()-1);   
  142.     }
  143.     
  144.     public void RunCmd(){
  145.         for(int i = 0;i<vec.size();i++)
  146.             ((Command)vec.elementAt(i)).execute();
  147.     }
  148.     
  149. }
  150. class Invoker{
  151.     Vector vec_cmd;
  152.     
  153.     public Invoker(){
  154.         
  155.         vec_cmd = new Vector();
  156.     }
  157.     
  158.     public void setCommand(Command c){      
  159.         vec_cmd.add(c);
  160.         invoke();
  161.     }
  162.     
  163.     public void undo(){
  164.         vec_cmd.remove(vec_cmd.size()-1);
  165.         ((Command)vec_cmd.elementAt(vec_cmd.size()-1)).execute();
  166.     }
  167.     public void invoke(){
  168.         Command cmd = (Command)vec_cmd.elementAt(vec_cmd.size()-1);
  169.         if(cmd != null)
  170.             cmd.execute();
  171.     }
  172. }
  173. public class CommandTest {//客户端?
  174.     public static void main(String[] args) {
  175.         // TODO 自动生成方法存根
  176.         
  177.         CommandStream CS = new CommandStream(); 
  178.         
  179.         RoomLight rl = new RoomLight();
  180.         KitchenLight kl = new KitchenLight();       //命令接收端
  181.         
  182.         CS.addCommand(new LightOnCommand(rl));      //一串命令堆起来执行,也可以用队列实现顺序执行(批处理)
  183.         CS.addCommand(new LightOnCommand(kl));
  184.         CS.addCommand(new LightOffCommand(kl));     
  185.         CS.addCommand(new MusicOnCommand());
  186.         
  187.         CS.RunCmd();
  188.         
  189.         Invoker in = new Invoker();                 //调用者,执行者
  190.         in.setCommand(new MusicOffCommand());
  191.         in.setCommand(new MusicOnCommand());
  192.         
  193.         in.setCommand(new LightTurnCommand(rl,5));  //具体命令
  194.         in.setCommand(new LightTurnCommand(rl,4));
  195.         
  196.         in.undo();
  197.         in.undo();
  198. /*
  199.         RoomLight1 rl1 = new RoomLight1();
  200.         rl1.On();
  201.         rl1.Off();
  202.         
  203.         Music mm = new Music();
  204.         mm.On();
  205.         mm.Off();
  206.         
  207. */
  208.     }
  209. }
  210. /*
  211. public class CommandTest{
  212.     
  213.     public enum level{low,mid,high,max}
  214.     
  215.     
  216.     public static void main(String[] args) {
  217.     
  218.         for(level l = level.low;l<level.max;)
  219.             System.out.println(l.toString());
  220.         
  221.         System.out.println(level.max.ordinal());
  222.         
  223.         level l = level.low;
  224.         switch(l){
  225.         case mid:
  226.             System.out.println("mid");
  227.             break;
  228.         case low:
  229.             System.out.println("low");
  230.             break;
  231.             
  232.         }
  233.     }
  234.     
  235. }
  236. */
  237. /*
  238. class Batch{                //如果不使用命令模式,暂时还真没想到怎么实现批处理,undo操作(除非用字符串来辨别,大堆ifelse(好像还不行))
  239.     Vector vec;
  240.     public Batch(){
  241.         vec = new Vector();
  242.     }
  243.     
  244.     public addCtrl(String str)
  245.     {
  246.         if(str == "RoomLight on"){
  247.             
  248.         }
  249.     }
  250. }
  251. class RoomLight1 extends Light{
  252.     
  253.     public RoomLight1(){
  254.         super("RoomLight1");
  255.     }
  256.     
  257.     public void On(){
  258.         turnTo(10);
  259.         print();
  260.     }
  261.     
  262.     public void Off(){
  263.         turnTo(0);
  264.         print();
  265.     }
  266. }
  267. class Music{
  268.     
  269.     public void On(){
  270.         System.out.println("music on");
  271.     }
  272.     public void Off(){
  273.         System.out.println("music off");
  274.     }
  275. }
  276. */
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值