⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 设计模式描述设计模式描述设计模式描述设计模式描述
💻 JAVA
字号:
/*
 * Created on 2005-5-11
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.test.pattern.behavir.memento;

/**
 * @author Administrator
 * 
 * 意图 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。 适用性
 * 必须保存一个对象在某一个时刻的(部分)状态, 这样以后需要时它才能恢复到先前的状态。
 * 如果一个用接口来让其它对象直接得到这些状态,将会暴露对象的实现细节并破坏对象的封装性。
 *  
 */
class Originator {
	private double manufacturer = 0;

	private double distributor = 0;

	private double retailer = 0;

	public void MakeSale(double purchasePrice) {
		// We assume sales are divided equally amount the three
		manufacturer += purchasePrice * .40;
		distributor += purchasePrice * .3;
		retailer += purchasePrice * .3;
		// Note: to avoid rounding errors for real money handling
		// apps, we should be using decimal integers
		// (but hey, this is just a demo!)
	}

	public Memento CreateMemento() {
		return (new Memento(manufacturer, distributor, retailer));
	}

	public void SetMemento(Memento m) {
		manufacturer = m.getA();
		distributor = m.getB();
		retailer = m.getC();
	}
}

class Memento {
	private double iA;

	private double iB;

	private double iC;

	public Memento(double a, double b, double c) {
		iA = a;
		iB = b;
		iC = c;
	}

	public double getA() {
		return iA;
	}

	public double getB() {
		return iB;
	}

	public double getC() {
		return iC;
	}
}

class caretaker {

}

/// <summary>
/// Summary description for Client.
/// </summary>

public class Client {
	public static void main(String[] args) {
		Originator o = new Originator();

		// Assume that during the course of running an application
		// we we set various data in the originator
		o.MakeSale(45.0);
		o.MakeSale(60.0);

		// Now we wish to record the state of the object
		Memento m = o.CreateMemento();

		// We make further changes to the object
		o.MakeSale(60.0);
		o.MakeSale(10.0);
		o.MakeSale(320.0);

		// Then we decide ot change our minds, and revert to the saved state
		// (and lose the changes since then)
		o.SetMemento(m);

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -