1 解决问题:
一些对象的状态可能需要“回滚”,
于是用一个“memoto”保存原状态,必要的时候再返回
2 解决方法:
一个备忘录的生成者Original类,它其实就是那个“可能需要回滚状态”的类。里面有一个creatMemoto()方法和一个rollbackByMemoto()方法
一个备忘录类Memoto,存储Original对象的状态
一个备忘录的管理类CareTaker(),用于保管Memoto(封装)。它有一个memoto的域,并提供setter和getter
形如:
Original
public class Original {
private String state1;
private String state2;
public Memoto creatMemoto(){
Memoto m = new Memoto();
m.setState1(state1);
m.setState2(state2);
return m;
}
public void rollbackByMemoto(Memoto m){
this.state1 = m.getState1();
this.state2 = m.getState2();
}
public String getState1() {
return state1;
}
public void setState1(String state1) {
this.state1 = state1;
}
public String getState2() {
return state2;
}
public void setState2(String state2) {
this.state2 = state2;
}
public Original(String state1, String state2) {
super();
this.state1 = state1;
this.state2 = state2;
}
@Override
public String toString() {
return this.state1 + " - " + this.state2;
}
}
Memoto
public class Memoto {
private String state1;
private String state2;
public String getState1() {
return state1;
}
public void setState1(String state1) {
this.state1 = state1;
}
public String getState2() {
return state2;
}
public void setState2(String state2) {
this.state2 = state2;
}
}
CareTaker
public class CareTaker {
Memoto memoto ;
public Memoto getMemoto() {
return memoto;
}
public void setMemoto(Memoto memoto) {
this.memoto = memoto;
}
}
Client
public class Demo {
public static void main(String[] args) {
Original o = new Original("11","22");
CareTaker t = new CareTaker();
t.setMemoto(o.creatMemoto());
System.out.println(o);
o.setState1("33");
o.setState2("44");
System.out.println(o);
o.rollbackByMemoto(t.getMemoto());
System.out.println(o);
}
}
======================进阶===========================
4问题提出:
在第一篇备忘录模式里,Memoto可以给Original和外界同样的权限,这是违反封装原则的。
5 解决方法:
宽接口和窄接口
两种思路:
a 对Memoto实现不同权限的接口
b 使用内部类
6 实现:
第一种:
如何实现???
第二种:
public class Original {
private String state1;
private String state2;
public Memoto creatMemoto(){
Memoto m = new Memoto();
m.setState1(state1);
m.setState2(state2);
return m;
}
public void rollbackByMemoto(NerrowMemoto m){
this.state1 = ((Memoto)m).getState1();
this.state2 = ((Memoto)m).getState2();
}
public String getState1() {
return state1;
}
public void setState1(String state1) {
this.state1 = state1;
}
public String getState2() {
return state2;
}
public void setState2(String state2) {
this.state2 = state2;
}
public Original(String state1, String state2) {
super();
this.state1 = state1;
this.state2 = state2;
}
@Override
public String toString() {
return this.state1 + " - " + this.state2;
}
//*********inner class********************
protected class Memoto implements NerrowMemoto{
private String state1;
private String state2;
private String getState1() {
return state1;
}
private void setState1(String state1) {
this.state1 = state1;
}
private String getState2() {
return state2;
}
private void setState2(String state2) {
this.state2 = state2;
}
}
}
public interface NerrowMemoto {
}
public class CareTaker {
NerrowMemoto memoto ;
public NerrowMemoto getMemoto() {
return memoto;
}
public void setMemoto(NerrowMemoto memoto) {
this.memoto = memoto;
}
}
public class Demo {
public static void main(String[] args) {
Original o = new Original("11","22");
CareTaker t = new CareTaker();
t.setMemoto(o.creatMemoto());
System.out.println(o);
o.setState1("33");
o.setState2("44");
System.out.println(o);
o.rollbackByMemoto(t.getMemoto());
System.out.println(o);
}
}