비망록 모드 와 명령 모드 의 결합

6433 단어 디자인 모드
/**  
 *      
 */
public interface Memento {

}
public interface OperationApi {
    /**
     *           
     */
    public int getResult(); 
    
    /**
     *     
     */
    public void add(int num);
    
    /**
     *     
     */
    public void substract(int num);
    
    /**
     *                  
     */
    public Memento createMemento();
    
    /**
     *             ,            
     */
    public void setMemento(Memento memento);
    
}
/**
 *         
 */
public interface Command {
     /**
     *     
     */
    public void execute();
     /**
     *     ,           
     */
    public void undo(Memento m);
     /**
     *     ,           
     */
    public void redo(Memento m);
     /**
     *        
     */
    public Memento createMemento();
}
public class Operation implements OperationApi {
    //       
	private int result;
	@Override
	public int getResult() {
		return result;
	}

	@Override
	public void add(int num) {
         result += num;
	}

	@Override
	public void substract(int num) {
         result -=num;
	}

	@Override
	public Memento createMemento() {
		MementoImpl m = new MementoImpl(result);
		return m;
	}

	@Override
	public void setMemento(Memento memento) {
        MementoImpl m = (MementoImpl) memento;
        this.result = m.getResult();
	}
	
	
	private static class MementoImpl implements Memento{
		private int result = 0;
		public MementoImpl(int result){
			this.result = result;
		}
		public int getResult(){
			return this.result;
		}
	} 
}
public abstract class AbstractCommand implements Command {
    /**
     *             
     */
    protected OperationApi operation =null;
	public void setOperation(OperationApi operation){
		this.operation = operation;
	}
	
	//       ,    
	@Override
	public void execute() {

	}
    
	@Override
	public void undo(Memento memento) {
          this.operation.setMemento(memento);
	}

	@Override
	public void redo(Memento memento) {
          this.operation.setMemento(memento);
	}

	@Override
	public Memento createMemento() {
		return this.operation.createMemento();
	}

}
public class AddCommand extends AbstractCommand {
       private int opeNum;
       
       public AddCommand(int opeNum){
    	   this.opeNum = opeNum;
       }

	@Override
	public void execute() {
		 this.operation.add(opeNum);
	} 
}
public class SubstractCommand extends AbstractCommand {
       private int opeNum;
       public SubstractCommand(int opeNum){
    	   this.opeNum = opeNum;
       }
       public void execute(){
    	   this.operation.substract(opeNum);
       }
}
/**
 *    ,     ,    ,         
 */
public class Caculator {
    /**
     *          ,       
     */
    private List undoCommands = new ArrayList();

    /**
     *           ,      
     */
    private List redoCommands = new ArrayList();

    /**
     *                ,     
     *        ,           ,           
     */
    private List undoMementos = new ArrayList();
    
    /**
     *             ,      ,
     *        ,            ,            
     */
    private List redoMementos = new ArrayList();
    
    
    
    
    private Command addCommand;
    private Command substractCommand;
    public void setAddCmd(Command addCommand){
    	this.addCommand = addCommand;
    }
    public void setSubstractCommand(Command substractCommand){
    	this.substractCommand = substractCommand;
    }
    
    
    //    
    public void addPressed(){
    	Memento m1 = this.addCommand.createMemento(); //        
    	this.addCommand.execute();//    
    	undoCommands.add(addCommand);//             
    	Memento m2 = this.addCommand.createMemento();//        
    	this.undoMementos.add(new Memento[]{m1,m2});//         
    }
    
    //    
    public void substractPressed(){
    	Memento m1 = this.substractCommand.createMemento();
    	this.substractCommand.execute();
    	undoCommands.add(this.substractCommand);
    	Memento m2 = this.substractCommand.createMemento();
    	this.undoMementos.add(new Memento[]{m1,m2});
    }
    
    
    
    public void undoPressed(){
    	if(undoCommands.size()>0){
    		Command cmd = undoCommands.get(undoCommands.size()-1);
    		Memento[] ms = undoMementos.get(undoCommands.size()-1);
    		//  
    		cmd.undo(ms[0]);
    		//          
    		redoCommands.add(cmd);
    		redoMementos.add(ms);
    		//       
    		undoCommands.remove(cmd);
    		undoMementos.remove(ms);
    	}else{
    		System.out.println("         ");
    	}
    }
    
    
    public void redoPressed(){
    	if(redoCommands.size()>0){
    		Command cmd = redoCommands.get(redoCommands.size()-1);
    		Memento[] ms = redoMementos.get(redoCommands.size()-1);
    		
    		cmd.redo(ms[1]);
    		undoCommands.add(cmd);
    		undoMementos.add(ms);
    		
    		redoCommands.remove(cmd);
    		redoMementos.remove(ms);
    	}else{
    		System.out.println("         ");
    	}
    } 
}
 
public class Client {

	public static void main(String[] args) {
        OperationApi operation = new Operation();
        AddCommand addCmd =new AddCommand(5);
        SubstractCommand substractCmd = new SubstractCommand(2);
        addCmd.setOperation(operation);
        substractCmd.setOperation(operation);
        
        
        Caculator caculator = new Caculator();
        caculator.setAddCmd(addCmd);
        caculator.setSubstractCommand(substractCmd);
        
        
        caculator.addPressed();
        System.out.println("          "+operation.getResult());
        caculator.substractPressed();
        System.out.println("          "+operation.getResult());
        caculator.undoPressed();
        System.out.println("        "+operation.getResult());
        caculator.undoPressed();
         System.out.println("         "+operation.getResult());
        caculator.redoPressed();
        System.out.println("        "+operation.getResult()); 
        caculator.redoPressed();
        System.out.println("         "+operation.getResult());
        
        
	}

}

좋은 웹페이지 즐겨찾기