public class History { private String[] recorders = {"太史伯","太史仲","太史叔","太史季","南史氏"}; private int record_num = 0;//被崔杼杀害的史官数量 private String truth = "崔杼弑其君"; public History(int record_num) { this.record_num = record_num; } /** * 发生 */ public void happen(){ Log.e("历史事件发生",truth); } /** * 记录 */ public Memoto record(){ Log.e(recorders[record_num] + "书曰",truth); Memoto memoto = new Memoto(truth); return memoto; } /** * 史官遇害 */ public void killRecorder(){ truth = "暴病而死"; Log.e("崔杼杀害"+recorders[record_num] + ",妄图篡改历史",truth); } /** * 前赴后继 */ public void recordAgain(Memoto memoto){ this.truth = memoto.getTruth(); Log.e(recorders[record_num] + "嗣书",truth); } }
记载真相的备忘录:
public class Memoto { private String truth; public Memoto(String truth) { this.truth = truth; } public String getTruth() { return truth; } }
备忘录的操作者Caretaker:
public class Caretaker { Memoto memoto; public void archive(Memoto memoto){ this.memoto = memoto; } public Memoto getMemoto(){ return memoto; } }
“在齐太史简”的全过程:
History history_0 = new History(0); history_0.happen(); Caretaker caretaker = new Caretaker(); caretaker.archive(history_0.record()); history_0.killRecorder(); History history_1 = new History(1); history_1.recordAgain(caretaker.getMemoto()); history_1.killRecorder(); History history_2 = new History(2); history_2.recordAgain(caretaker.getMemoto()); history_2.killRecorder(); History history_3 = new History(3); history_3.recordAgain(caretaker.getMemoto());