Memento Design [메멘토 디자인]

  1. Design Object [디자인목적]

    1. Undo 와 같은 과거 상태를 복구해야 하는 경우 , 과거 상태를 저장하는 디자인
  2. Options [대안들]

  3. Benefit [디자인 장점]

    1. 객체 내부 상태 정보를 저장하여 별도 클래스로 저장

    2. [단점] 저장할 정보가 많을 경우, 객체가 많이 생성되어 메모리관리에 문제가 발생할수 있음.

  4. Structure & Sample Code [구조와 샘플코드]

    1) 선 그리기와 상태저장, undo

    1. UML

    2. 구조

      -> ConnectionSolver 객체 : 실제적으로 작업을 하는 객

      1. 사용자[client]에 의해 지속적으로 변화가 되어 undo를 기억해야 하는 객체 - client에 의해 사용되어 지는 객체

      -> ConnectionSolverCaretaker 객체 : ConnectionSolver의 상태를 저장하는 Memento객체를 생성 및 불러오는 객체[관리자]

      1. Memento의 생성과 복구는 모두 caretaker에 의해 관리

      -> ConnectionSolverMemento 객체 : caretaker.saveState에 의해 생성되며, ConnectionSolver의 attributes의 상태정보를 저장하고 있으며, caretaker.restoerState에 의해 복구됨 .

  5. Source code

    1. Client
package behavioral.memento.improved;


import java.awt.geom.Point2D;

public class Client {

    public static void main(String[] args) {
        ConnectionSolver connectionSolver = new ConnectionSolver();
        ConnectionSolverCaretaker caretaker = new ConnectionSolverCaretaker();

        // First state change in ConnectionSolver
        connectionSolver.setFirstPoint(new Point2D.Double(1.0,1.0));
        connectionSolver.setSecondPoint(new Point2D.Double(5.0, 1.0));

        // Create Memento to saving first state
        caretaker.saveState(connectionSolver);

        connectionSolver.calculateLine();

        // Change initial state of ConnectionSovler
        connectionSolver.setFirstPoint(new Point2D.Double(2.0,2.0));
        connectionSolver.setSecondPoint(new Point2D.Double(5.0, 1.0));

        connectionSolver.calculateLine();

        // Create new ConnectionSolver object for restoring
        ConnectionSolver newConnectionSolver = new ConnectionSolver();

        // Restore initial state
        caretaker.restoreState(newConnectionSolver);

        newConnectionSolver.calculateLine();

        caretaker.restoreState(connectionSolver);

        connectionSolver.calculateLine();

    }

}
  • ConnectionSolver
package behavioral.memento.improved;
import java.awt.geom.Point2D;

// Draw a line
public class ConnectionSolver {

    private Point2D firstPoint;
    private Point2D secondPoint;

    // Draw a line
    public void calculateLine(){
         System.out.println("Calculate line between: "+firstPoint+" and "+secondPoint);

    }

    // Create Memento
    public ConnectionSolverMemento createMemento(){
        return new ConnectionSolverMemento(firstPoint,secondPoint);
    }

    public void setMemento(ConnectionSolverMemento memento){
        firstPoint = memento.getFirstPoint();
        secondPoint = memento.getSecondPoint();
    }

    public void setFirstPoint(Point2D firstPoint) {
        this.firstPoint = firstPoint;
    }

    public void setSecondPoint(Point2D secondPoint) {
        this.secondPoint = secondPoint;
    }
}
  • ConnectionSolverCaretake
package behavioral.memento.improved;


public class ConnectionSolverCaretaker {

    private ConnectionSolverMemento connectionSolverMemento;

    public void saveState(ConnectionSolver connectionSolver){
        connectionSolverMemento = connectionSolver.createMemento();
    }

    public void restoreState(ConnectionSolver connectionSolver){

        connectionSolver.setMemento(connectionSolverMemento);

    }


}
  • ConnectionSolverMemento
package behavioral.memento.improved;
import java.awt.geom.Point2D;

public class ConnectionSolverMemento {

    private Point2D firstPoint;

    private Point2D secondPoint;


    public ConnectionSolverMemento(Point2D firstPoint, Point2D secondPoint) {
        this.firstPoint = firstPoint;
        this.secondPoint = secondPoint;
    }

    public Point2D getFirstPoint() {
        return firstPoint;
    }

    public Point2D getSecondPoint() {
        return secondPoint;
    }
}

results matching ""

    No results matching ""