首页 > 代码库 > Marketing profits of Non woven bags business

Marketing profits of Non woven bags business

//备忘录模式定义:
//在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。
//这样以后就可以将该对象恢复到原先保存的状态
//实例:测试两种方案,两种方案在第一阶段的过程是相同的,第二阶段是不同的
//实例代码
//备忘录对象的窄接口
public interface FlowAMockMemento {
	//空的,所谓窄接口,即只是一个标识作用,它的持有者不可以调用任何它的方法
}
//测试流程类
public class FlowAMock {
	private String flowName;
	private int tempResult;
	private String tempState;
	public FlowAMock(String flowName) {
		this.flowName = flowName;
	}
	public void runPhaseOne() {
		tempResult = 3;
		tempState = "PhaseOne";
	}
	public void shema1() {
		this.tempState += ",Schema1";
		System.out.pritnln(this.tempState+":now run"+tempResult);
		this.tempResult += 11;
	}
	public void schema2() {
		this.tempState += ",Schema2";
		System.out.pritnln(this.tempState+":now run"+tempResult);
		this.tempResult += 22;
	}
	public FlowAMockMemento createMemento() {
		return new MementoImpl(this.tempResult, this.tempState);
	}
	public void setMemento(FlowAMockMemento memento) {
		MementoImpl mementoImpl = (MementoImpl)memento;
		this.tempResult = mementoImpl.getTempResult();
		this.tempState = mementoImpl.getTempState();
	}
	private static class MemetoImpl implements FlowAMockMemento	{
		private int tempResult;
		private String tempState;
		public MemetoImpl(int tempResult, String tempState) {
			this.tempResult = tempResult;
			this.tempState = tempState;
		}
		public int getTempResult() {
			return tempResult;
		}
		public int getTempState() {
			return tempState;
		}
	}
}
//备忘录管理类
public class FlowAMementoCareTaker {
	private FlowAMockMemento memento = null;
	public void saveMemento(FlowAMockMemento memento) {
		this.memento = memento;
	}
	public FlowAMockMemento retriveMemento() {
		return this.memento;
	}
}
//客户端
public class Client {
	public static void main(String[] args) {
		FlowAMock mock = new FlowAMock("TestFlow");
		mock.runPhaseOne();
		FlowAMementoCareTaker careTaker = new FlowAMementoCareTaker();
		FlowAMockMemento memento = mock.createMemento();
		careTaker.saveMemento(memento);
		mock.schema1();//运行第一种方案
		mock.setMemento(careTaker.retriveMemento());//恢复数据
		mock.schema2();//运行第二种发难
	}
}
//备忘录模式本质:保存和恢复内部状态
//优点:更好的封装性,由于使用了内部类和窄接口,所以外部就算持有备忘录接口对象也无法访问其中的属性
//备忘录模式中内部内和窄接口是最给力的设计,这样给把对象的接口给外部保存
//但是接口中没有方法,所以外部无法访问,充分保证了封装性
//只有创建它的原发器才可以设置和获取它的值