- /*
- * 关于命令模式的个人理解,不是很深透,先写篇短文记录一下学习历程,这几天陆续重写早段时间看的其他几个模式
- * 命令模式
- * 主要有几个角色:client,接受者,抽象命令接口,具体命令实现,调用者
- * 下面写了个例子:
- * Light 抽象灯类-接受者
- * RoomLight,KitchenLight 具体类
- *
- * Command 抽象命令接口
- * LightOnCommand ....具体实现命令接口
- *
- * CommandStream 个人感觉比较类似于批处理一样的东西,可以任意添加删除命令,最后按一个键RunCmd就可以实现
- * 整串命令实行,各自调用各自的execute方法,具体如何实现还看各具体命令自定义的方法
- *
- * Invoker 调用者,用于调用命令,setcommand触发当时要执行的命令
- * undo操作等都在代码中描述
- *
- * 对于命令模式,经过一点思考,总是想:如果不用命令模式,代码会变成怎么样》???
- * 这个我觉得是关键问题,如果知道为什么要用命令模式,就会在以后可以灵活用到此东西
- * 所以在查了一些资料和自己想了一些代码之后(后面注释的就是我想不用此模式写的代码)
- * 可以看到:
- * RoomLight1内部有on off等方法,如果要调用,还是可以就是new一个roomlight1出来,然后调用on之类东西
- * 但是要实现上面的类似于CommandStream,还真的不知如何实现,如果在一个类中的一串命令,还好说,直接就on-off-turnto(2)-...
- * 但是如果多起来,而且经常变动的话,也不好做!!
- * 而因为在不同类中,如misic,roomlight1之间不同的命令,如何来区分不同的实例??(这是一个问题),
- * 如果类中的方法在后来会扩展,变多的话,想了一下,改动会很复杂
- * 另外,做undo等操作时,如何做??这也是比较麻烦的事!!
- *
- * 所以,按照一些文章所说的:命令模式就将命令提升到类的层次,这样可以应对各种变化,调用者不用知道如何实现,只知道我要调用就ok
- * 实现的也不用管谁来调用我,我只管我自己实现就行了!!
- *
- * 上面是一些个人看法,理解不深!!
- * 以后用到会加深理解
- * 待续。。。。
- *
- */
- package command_pack;
- import java.util.*;
- abstract class Light{
- private String lightname;
- private int level;
- public Light(String str){
- lightname = str;
- level = 0;
- }
- public void turnTo(int s){
- level = s;
- }
- public int getLevel(){
- return level;
- }
- public String getName(){
- return lightname;
- }
- public void print(){
- System.out.println(this.getName()+" :light level " + this.getLevel());
- }
- }
- class RoomLight extends Light{
- public RoomLight(){
- super("roomLight");
- }
- }
- class KitchenLight extends Light{
- public KitchenLight(){
- super("kitchenLight");
- }
- }
- class LightOnCommand implements Command{
- private Light light;
- public LightOnCommand(Light l){ //带参数命令模式
- light = l;
- }
- public void execute(){
- light.turnTo(10); //10 is the max level for simulation
- light.print();
- }
- }
- class LightTurnCommand implements Command{
- private Light light;
- private int level;
- public LightTurnCommand(Light li,int le){
- light = li;
- level = le;
- }
- public void execute(){
- light.turnTo(level);
- light.print();
- }
- }
- class LightOffCommand implements Command{
- private Light light;
- public LightOffCommand(Light l){
- light = l;
- }
- public void execute(){
- light.turnTo(0);
- light.print();
- }
- }
- class MusicOnCommand implements Command{ //不带参数命令模式
- public void execute(){
- System.out.println("Music on ");
- }
- }
- class MusicOffCommand implements Command{
- public void execute(){
- System.out.println("Music off ");
- }
- }
- class CommandStream{ //命令流
- private Vector vec;
- public CommandStream(){
- vec = new Vector();
- }
- public void addCommand(Command cmd){
- vec.add(cmd);
- }
- public void removeCommand(){
- vec.remove(vec.size()-1);
- }
- public void RunCmd(){
- for(int i = 0;i<vec.size();i++)
- ((Command)vec.elementAt(i)).execute();
- }
- }
- class Invoker{
- Vector vec_cmd;
- public Invoker(){
- vec_cmd = new Vector();
- }
- public void setCommand(Command c){
- vec_cmd.add(c);
- invoke();
- }
- public void undo(){
- vec_cmd.remove(vec_cmd.size()-1);
- ((Command)vec_cmd.elementAt(vec_cmd.size()-1)).execute();
- }
- public void invoke(){
- Command cmd = (Command)vec_cmd.elementAt(vec_cmd.size()-1);
- if(cmd != null)
- cmd.execute();
- }
- }
- public class CommandTest {//客户端?
- public static void main(String[] args) {
- // TODO 自动生成方法存根
- CommandStream CS = new CommandStream();
- RoomLight rl = new RoomLight();
- KitchenLight kl = new KitchenLight(); //命令接收端
- CS.addCommand(new LightOnCommand(rl)); //一串命令堆起来执行,也可以用队列实现顺序执行(批处理)
- CS.addCommand(new LightOnCommand(kl));
- CS.addCommand(new LightOffCommand(kl));
- CS.addCommand(new MusicOnCommand());
- CS.RunCmd();
- Invoker in = new Invoker(); //调用者,执行者
- in.setCommand(new MusicOffCommand());
- in.setCommand(new MusicOnCommand());
- in.setCommand(new LightTurnCommand(rl,5)); //具体命令
- in.setCommand(new LightTurnCommand(rl,4));
- in.undo();
- in.undo();
- /*
- RoomLight1 rl1 = new RoomLight1();
- rl1.On();
- rl1.Off();
- Music mm = new Music();
- mm.On();
- mm.Off();
- */
- }
- }
- /*
- public class CommandTest{
- public enum level{low,mid,high,max}
- public static void main(String[] args) {
- for(level l = level.low;l<level.max;)
- System.out.println(l.toString());
- System.out.println(level.max.ordinal());
- level l = level.low;
- switch(l){
- case mid:
- System.out.println("mid");
- break;
- case low:
- System.out.println("low");
- break;
- }
- }
- }
- */
- /*
- class Batch{ //如果不使用命令模式,暂时还真没想到怎么实现批处理,undo操作(除非用字符串来辨别,大堆ifelse(好像还不行))
- Vector vec;
- public Batch(){
- vec = new Vector();
- }
- public addCtrl(String str)
- {
- if(str == "RoomLight on"){
- }
- }
- }
- class RoomLight1 extends Light{
- public RoomLight1(){
- super("RoomLight1");
- }
- public void On(){
- turnTo(10);
- print();
- }
- public void Off(){
- turnTo(0);
- print();
- }
- }
- class Music{
- public void On(){
- System.out.println("music on");
- }
- public void Off(){
- System.out.println("music off");
- }
- }
- */
命令模式
最新推荐文章于 2024-08-30 16:00:44 发布