메멘토 패턴

참가자들
발신자 이외의 개체에 의한 액세스로부터 보호합니다. Mementos에는 사실상 두 개의 인터페이스가 있습니다. Caretaker는 Memento에 대한 좁은 인터페이스를 봅니다. 다른 개체에 memento를 전달할 수만 있습니다. 반대로 Originator는 이전 상태로 복원하는 데 필요한 모든 데이터에 액세스할 수 있는 넓은 인터페이스를 봅니다. 이상적으로는 memento를 생성하는 생성자만 memento의 내부 상태에 액세스할 수 있습니다.
기념품을 사용하여 내부 상태를 복원합니다
유품의 내용을 조작하거나 조사하지 마십시오.
암호
 public class Main {
    public static void main(String[] args) {
        Originator o = new Originator();
        o.setState("On");
        Caretaker c = new Caretaker();
        c.setMemento(o.createMemento());
        o.setState("Off");
        o.setMemento(c.getMemento());
    }
}
public class Originator {
    private String state;
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
        System.out.println("State = " + state);
    }
    public Memento createMemento() {
        return new Memento(state);
    }
    public void setMemento(Memento memento) {
        System.out.println("Restoring state...");
        setState(memento.getState());
    }
}
public class Memento {
    private String state;
    public Memento(String state) {
        this.state = state;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}
public class Caretaker {
    private Memento memento;
    public Memento getMemento() {
        return memento;
    }
    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}
 산출
 State = On
State = Off
Restoring state...
State = On
  
     eidherjulian61
        /
        디자인 패턴
     
      
    
  
   
    
       
    
  
   
    
      주요 디자인 패턴
      eidher ・ 2020년 9월 27일 ・ 1분 읽기
      
        #designpatterns
        #creational
        #structural
        #behavioral
      
    
  
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(메멘토 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://dev.to/eidher/memento-pattern-441i
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
public class Main {
    public static void main(String[] args) {
        Originator o = new Originator();
        o.setState("On");
        Caretaker c = new Caretaker();
        c.setMemento(o.createMemento());
        o.setState("Off");
        o.setMemento(c.getMemento());
    }
}
public class Originator {
    private String state;
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
        System.out.println("State = " + state);
    }
    public Memento createMemento() {
        return new Memento(state);
    }
    public void setMemento(Memento memento) {
        System.out.println("Restoring state...");
        setState(memento.getState());
    }
}
public class Memento {
    private String state;
    public Memento(String state) {
        this.state = state;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}
public class Caretaker {
    private Memento memento;
    public Memento getMemento() {
        return memento;
    }
    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}
State = On
State = Off
Restoring state...
State = On
eidherjulian61 / 디자인 패턴
 
    주요 디자인 패턴
eidher ・ 2020년 9월 27일 ・ 1분 읽기
        #designpatterns
        #creational
        #structural
        #behavioral
      
    Reference
이 문제에 관하여(메멘토 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/eidher/memento-pattern-441i텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)