메멘토 패턴 (스킵 된 게터 및 세터)의 일반적인 구현 예입니다.메멘토 패턴 단점
public class Employee {
private String name;
private String phone;
public EmployeeMemento save() {
return new EmployeeMemento(name, phone);
}
public void revert(EmployeeMemento memento) {
this.name = memento.getName();
this.phone = memento.getPhone();
}
}
public class EmployeeMemento {
private final String name;
private final String phone;
public EmployeeMemento(String name, String phone) {
this.name = name;
this.phone = phone;
}
}
public class Caretaker {
private Stack<EmployeeMemento> history;
public Caretaker() {
history = new Stack<>();
}
public void save(Employee employee) {
history.push(employee.save());
}
public void revert(Employee employee) {
employee.revert(history.pop());
}
}
발견 된이 패턴의 모든 구현은 위의 것과 거의 동일합니다. 그러나 구현이 종류에 대한 몇 가지 문제는 내가 좋아하는하지 않는 것이있다 :
- 그것은
employee.revert()
및caretaker.revert(employee)
모두 triger 가능합니다. 나는 오직 하나의 액세스 포인트를 원한다. - EmployeeMemento를 변경하려면 Employee 클래스에서 변경해야합니다 (
revert
메서드이기 때문에).
이 문제를 극복 할 방법이 있습니까? 어쩌면 너무 많은 관심을 기울일 수 있습니다.이 세부 사항은 그리 중요하지 않습니다.