From 6d121af8d3ad62c1a370b70cc333548ccb56a960 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Tue, 5 Jun 2018 15:28:04 +0800 Subject: [PATCH] auto commit --- notes/设计模式.md | 169 +++++++++++++++++- pics/867e93eb-3161-4f39-b2d2-c0cd3788e194.png | Bin 0 -> 11264 bytes 2 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 pics/867e93eb-3161-4f39-b2d2-c0cd3788e194.png diff --git a/notes/设计模式.md b/notes/设计模式.md index 17505b60..35bddad5 100644 --- a/notes/设计模式.md +++ b/notes/设计模式.md @@ -1204,9 +1204,176 @@ doSprinkler() 在不违反封装的情况下获得对象的内部状态,从而在需要时可以将对象恢复到最初状态。 +### 类图 + +- Originator:原始对象 +- Caretaker:负责保存好备忘录 +- Menento:备忘录,存储原始对象的的状态。备忘录实际上有两个接口,一个是提供给 Caretaker 的窄接口:它只能将备忘录传递给其它对象;一个是提供给 Originator 的宽接口,允许它访问到先前状态所需的所有数据。理想情况是只允许 Originator 访问本备忘录的内部状态。 + +

+ +### 实现 + +以下实现了一个简单计算器程序,可以输入两个值,然后计算这两个值的和。备忘录模式允许将这两个值存储起来,然后在某个时刻用存储的状态进行恢复。 + +实现参考:[Memento Pattern - Calculator Example - Java Sourcecode](https://www.oodesign.com/memento-pattern-calculator-example-java-sourcecode.html) + +```java +/** + * Originator Interface + */ +public interface Calculator { + + // Create Memento + PreviousCalculationToCareTaker backupLastCalculation(); + + // setMemento + void restorePreviousCalculation(PreviousCalculationToCareTaker memento); + + int getCalculationResult(); + + void setFirstNumber(int firstNumber); + + void setSecondNumber(int secondNumber); +} +``` + +```java +/** + * Originator Implementation + */ +public class CalculatorImp implements Calculator { + + private int firstNumber; + private int secondNumber; + + @Override + public PreviousCalculationToCareTaker backupLastCalculation() { + // create a memento object used for restoring two numbers + return new PreviousCalculationImp(firstNumber, secondNumber); + } + + @Override + public void restorePreviousCalculation(PreviousCalculationToCareTaker memento) { + this.firstNumber = ((PreviousCalculationToOriginator) memento).getFirstNumber(); + this.secondNumber = ((PreviousCalculationToOriginator) memento).getSecondNumber(); + } + + @Override + public int getCalculationResult() { + // result is adding two numbers + return firstNumber + secondNumber; + } + + @Override + public void setFirstNumber(int firstNumber) { + this.firstNumber = firstNumber; + } + + @Override + public void setSecondNumber(int secondNumber) { + this.secondNumber = secondNumber; + } +} +``` + +```java +/** + * Memento Interface to Originator + * + * This interface allows the originator to restore its state + */ +public interface PreviousCalculationToOriginator { + int getFirstNumber(); + int getSecondNumber(); +} +``` + +```java +/** + * Memento interface to CalculatorOperator (Caretaker) + */ +public interface PreviousCalculationToCareTaker { + // no operations permitted for the caretaker +} +``` + +```java +/** + * Memento Object Implementation + *

+ * Note that this object implements both interfaces to Originator and CareTaker + */ +public class PreviousCalculationImp implements PreviousCalculationToCareTaker, + PreviousCalculationToOriginator { + + private int firstNumber; + private int secondNumber; + + public PreviousCalculationImp(int firstNumber, int secondNumber) { + this.firstNumber = firstNumber; + this.secondNumber = secondNumber; + } + + @Override + public int getFirstNumber() { + return firstNumber; + } + + @Override + public int getSecondNumber() { + return secondNumber; + } +} +``` + +```java +/** + * CareTaker object + */ +public class Client { + + public static void main(String[] args) { + // program starts + Calculator calculator = new CalculatorImp(); + + // assume user enters two numbers + calculator.setFirstNumber(10); + calculator.setSecondNumber(100); + + // find result + System.out.println(calculator.getCalculationResult()); + + // Store result of this calculation in case of error + PreviousCalculationToCareTaker memento = calculator.backupLastCalculation(); + + // user enters a number + calculator.setFirstNumber(17); + + // user enters a wrong second number and calculates result + calculator.setSecondNumber(-290); + + // calculate result + System.out.println(calculator.getCalculationResult()); + + // user hits CTRL + Z to undo last operation and see last result + calculator.restorePreviousCalculation(memento); + + // result restored + System.out.println(calculator.getCalculationResult()); + } +} +``` + +```html +110 +-273 +110 +``` + ### JDK -- [java.util.Date](http://docs.oracle.com/javase/8/docs/api/java/util/Date.html) +- java.io.Serializable ## 7. 观察者(Observer) diff --git a/pics/867e93eb-3161-4f39-b2d2-c0cd3788e194.png b/pics/867e93eb-3161-4f39-b2d2-c0cd3788e194.png new file mode 100644 index 0000000000000000000000000000000000000000..26cb0153f21019cadba1a74ad6b78e3cb03a177b GIT binary patch literal 11264 zcmeHtcTkgC|0gy;LFod5RHcc4l+c58P^utBI!L=jsUj_OB-DT)EkLB}6@>sI2vS2A zBA}8;lMWX!G=rh;$-Vdey=8aiH@ma{?966{kmSje=bZ03=hM!UU}B_u{@kT=R8&;w z_4TywP*I&kf!B4k)Zlr1cj*oIcf$XUt_IbcA?_9M=Cr%Ip*j^621$3|bOyXX>!WAw zPepa1lk#_>$NR;7Dk`x*^tIH@gY4G}XewQ`4|@`hwG2OdeByg??N+hYS$;iQTIMU2 zbTxV`RwgTam$S;wHQbV`sO1`aMGgWE8TaI3m3x_QwghaFR@nlSs!`8}EYGbtAU~uinfnf=dNl zGbm7!vvE$is^gahPH167ahyzdnyHF7^s3+c;}sI7FH^@ba^1tw%CGOLZoO713XF6J zeV1D>rEIN2?a;G+XXLhfl6Z|?&ZVVHGo!vE(8c6@(~m{*JbBmOP52Q? zkWi(XiW~Wzjkbbx4A{kYEk9qcO&=dUcKQXL<^9Tcn&1f6RO!Q}>BEQjy|dsO?KA|Z z_{$ebRd2uEBd8sdap{70*bsw~;hRZuuBjW8bLT(KL`%DQsP%@iqTp0IX^p4Z?E;3^ z+DvG{yBFN>9nPs6n_*2tm4A*sXPz>nu!I>6;n zD#3mme&5&i64=;ym{Qjp=2B-S9FDfK35nO=2@eD%8iUf}qHcX8Wu@C%CbX=|U&jL*A1^$Q<0l#iS9!Kv=v zN5N|JMP6|?fmze3D(XHQX!5mDa$)<|9Fl?eE%s`b>&fvoHDdF&#$z7RX7&Tp?zP0h z5Z2oTPJ3TWU?c?$vnaEJ>1SX1nH~p~=i-lp+Rw0`p2OFXvYPDdC0rz@X(EK)ySh4n}h-JT7w`7T#{j8=M(2zl^6ktvGRJIN%%@dUytnp z?`WH)wf(I?+MJJTsN8kP%3*=PgAG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