Chinaunix首页 | 论坛 | 博客
  • 博客访问: 703802
  • 博文数量: 147
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1725
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-22 10:36
文章分类

全部博文(147)

文章存档

2011年(1)

2010年(1)

2009年(35)

2008年(110)

我的朋友

分类: Java

2008-11-27 20:13:36

命令模式:
主要实现的是把客户的各种请求和操作封装到一个命令对象中,从而达到把命令的请求和对命令的
具体执行两者之间的关系相互分离的目标;同时还能对命令的请求者以统一的形式进行
命令请求(功能调用),并委派给不同的对象
用应:
在某个管理系统中,需要对每个登陆的用户进行日志记录,但是日志记录有多种形式,比如:
控制台,log4j
 

命令执行者的接口:
public interface TransferLogInterface {
public boolean doTransferLog(String logInfoString);
}

 

命令执行者的接口实现类之一
public class TransferLogBean implements TransferLogInterface {

    public boolean doTransferLog(String logInfoString) {
        // TODO Auto-generated method stub
        System.out.println(logInfoString);
        return true;
    }
}
命令执行者的接口实现类之2
public class TransferLog4JBean implements TransferLogInterface {

    public boolean doTransferLog(String logInfoString) {
        // TODO Auto-generated method stub
        System.out.println("this is a log4j");
        return true;

    }
}

 

命令调度类:
public class TransferLogCommand {
public final static int CONTRO_OUTPUT=1;
public final static int LOGJFJ_OUTPUT=2;
public static boolean produceTransferLogRequests(int requestIndex,String logInfoString){
    TransferLogInterface oneTransferBean=null;
    boolean executeRequest=false;
    switch(requestIndex){
     case CONTRO_OUTPUT:
         oneTransferBean=new TransferLogBean();
         executeRequest=oneTransferBean.doTransferLog(logInfoString);
         break;
     case LOGJFJ_OUTPUT:
         oneTransferBean=new TransferLog4JBean();
         executeRequest=oneTransferBean.doTransferLog(logInfoString);
         break;
    }
    return executeRequest;
}
}

 

客户调用:
public class Test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
   String info="jjjjjj";
   boolean executeRequest=TransferLogCommand.produceTransferLogRequests(TransferLogCommand.CONTRO_OUTPUT, info);   
}
}

 

用命令模式实现Redo/Undo功能

命令接口:

public interface CommandInterface {
public boolean executeCommand();
public boolean unDoCommand();
}

命令接口的各个实现类:

public class ExecuteEditCutCommand implements CommandInterface {
    public boolean executeCommand() {
        // TODO Auto-generated method stub

        System.out.println("do cut!");
        return true;
    }
    public boolean unDoCommand() {
        // TODO Auto-generated method stub

        System.out.println("undo cut!");
        return true;
    }
}

public class ExecuteEditDelCommand implements CommandInterface {

    public boolean executeCommand() {
        // TODO Auto-generated method stub

        System.out.println("do del");
        return true;
    }
    public boolean unDoCommand() {
        // TODO Auto-generated method stub

        System.out.println("undo del");
        return true;
    }
}

public class ExecuteEditUpdateCommand implements CommandInterface {

    public boolean executeCommand() {
        // TODO Auto-generated method stub

        System.out.println("do update!");
        return true;
    }
    public boolean unDoCommand() {
        // TODO Auto-generated method stub

        System.out.println("undo update!");
        return true;
    }
}

命令调度接口和命令指挥官类:

 

 

public interface CommandControerInterface {
public final int EditCutter=1;
public final int EditDelete=2;
public final int EditUpdate=3;
public final int EditUndo=4;
public final int EditRedo=5;
public boolean processEditCommand(int editCommand_Type);
}

public class CommandControler implements CommandControerInterface {
private List allUnDoCommandList=null;
private List allReDoCommandList=null;
private CommandInterface oneCommandExecuter=null;
private static CommandControerInterface oneCommandControler=null;
public CommandControler(){
    this.allReDoCommandList=new ArrayList();
    this.allUnDoCommandList=new ArrayList();
}
public static synchronized CommandControerInterface getCommandControler(){
    if(oneCommandControler==null){
        oneCommandControler=new CommandControler();
    }
    return oneCommandControler;
}
    public boolean processEditCommand(int editCommand_Type) {
        // TODO Auto-generated method stub

        boolean executeRequest=false;
        switch(editCommand_Type){
         case EditCutter:
             oneCommandExecuter=new ExecuteEditCutCommand();
             executeRequest=oneCommandExecuter.executeCommand();
     saveSomeOneCommand(oneCommandExecuter);
     break;
         case EditDelete:
             oneCommandExecuter=new ExecuteEditDelCommand();
             executeRequest=oneCommandExecuter.executeCommand();
     saveSomeOneCommand(oneCommandExecuter);
     break;
         case EditUpdate:
             oneCommandExecuter=new ExecuteEditUpdateCommand();
             executeRequest=oneCommandExecuter.executeCommand();
     saveSomeOneCommand(oneCommandExecuter);
     break;
         case EditUndo:
             executeRequest=unDoOneCommand();
             break;
         case EditRedo:
             executeRequest=reDoOneCommand();
             break;
        }
        return executeRequest;
    }
    public boolean unDoOneCommand(){
        if(allUnDoCommandList.size()==0){
            return true;
        }

CommandInterface oneCommand=((CommandInterface)allUnDoCommandList.get(allUnDoCommandList.size()-1)));

boolean executeRequest=oneCommand.unDoCommand();

allUnDoCommandList.remove(oneCommand);

allReDoCommandList.add(oneCommand);

return executeRequest;
    }

public boolean reDoOneCommand(){
        if(allReDoCommandList.size()==0){
            return true;
        }

CommandInterface oneCommand=((CommandInterface)allReDoCommandList.get(allReDoCommandList.size()-1)));

boolean executeRequest=oneCommand.reDomand();

allReDoCommandList.remove(oneCommand);

allUnDoCommandList.add(oneCommand);

return executeRequest;
    }

public void saveSomeOneCommand(CommandInterface oneCommand){
    allUnDoCommandList.add(oneCommand);
}
}

阅读(775) | 评论(0) | 转发(0) |
0

上一篇:sql mode

下一篇:字符集的使用

给主人留下些什么吧!~~