设计模式之备忘录模式

睡觉前就再讲一下备忘录模式吧呵呵。。

 备忘录模式所要完成的功能就是能做一个备忘的功能,可以把一个对象还原到这个对象上个状态的功能。说白了就是能向我们MyEclipse一样ctrl+z能恢复到上一步,接下来就用代码来说明下一这个备忘录模式:

package com.djk.design.备忘模式;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 备忘录模式
 * @author djk
 *
 */
public class MemoryTest2
{
	public static void main(String[] args) 
	{

		//创建一个转换接口
		ConvertUtilInterFace convertUtilInterFace = new MyConvertUtil();
		//创建一个人
		Person person = new Person(convertUtilInterFace);
		person.setAge("23");
		person.setName("张三");
		System.out.println("刚开始人的属性:"+person);
		
		//创建一个记录备忘录的东西
		MemoryRecord memoryRecord = new MemoryRecord();
		//创建一个备忘录
		memoryRecord.setMemoryMap("001", person.createMemory());
		
		//重新设置人的属性
		person.setAge("24");
		person.setName("李四");
		System.out.println("重新设置后人的属性:"+person);
		
		//进行还原
		Memory memory = memoryRecord.getMemory("001");
		person.restore(memory);
		System.out.println("还原后人的属性:"+person);
	}
}

/**
 * 人
 * @author djk
 *
 */
class Person 
{
	//姓名
	private String name;
	//年龄
	private String age;
	
	private ConvertUtilInterFace convertUtilInterFace;
	
	public Person(ConvertUtilInterFace convertUtilInterFace)
	{
		this.convertUtilInterFace = convertUtilInterFace;
	}

	//创建备忘录
	public Memory createMemory()
	{
		return new Memory(convertUtilInterFace.objectToMap(this));
	}
	//还原
	public void restore (Memory memory)
	{
		convertUtilInterFace.mapToObject(memory, this);
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}
	
	public String toString()
	{
		return "age:"+this.age+"name:"+this.name;
	}
}

/**
 * 备忘录
 * @author djk
 *
 */
class Memory
{
	private  Map<String,String> map = new HashMap<String, String>();

	
	public Memory(Map<String,String> map)
	{
		this.map = map;
	}
	
	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}
}

/**
 * 用来记录备忘录的
 * @author djk
 *
 */
class MemoryRecord
{
	private Map<String,Memory>  memoryMap = new HashMap<String, Memory>();
	
	public void setMemoryMap(String key,Memory memory) 
	{
		memoryMap.put(key,memory);
	}
	
	public Memory getMemory(String key)
	{
		if(null ==key)
		{
			return null;
		}
		
		return memoryMap.get(key);
	}

}

/**
 * 转化接口
 * @author djk
 *
 */
interface ConvertUtilInterFace
{
	Map<String,String> objectToMap(Object object);
	
	void mapToObject(Memory memory,Object object);
}

/**
 *  转换实现类
 * @author djk
 *
 */
class MyConvertUtil implements ConvertUtilInterFace
{
	
	public Map<String,String> objectToMap(Object object)
	{
		Map<String,String> result = new HashMap<String, String>();
		if(null == object)
		{
			return null;
		}
		
		Class objectClass = object.getClass();
		Method [] methods = objectClass.getDeclaredMethods();
		try {
			String methodName= null;
			for(Method method :methods)
			{
				 methodName = method.getName();
				if(methodName.startsWith(MyConstant.GET.getValue()))
				{
					String key = methodName.substring(3).toLowerCase();
					result.put(key, (String)method.invoke(object, new Object[]{}));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return result;
	}

	public void mapToObject(Memory memory, Object object)
	{
		if(null ==memory)
		{	
			return ;
		}
		Map<String,String> result = memory.getMap();
		
		if(null ==result)
		{	
			return ;
		}
		
		Class objectClass = object.getClass();
		
		Method [] methods = objectClass.getDeclaredMethods();
		try {
			String methodName = null;
			for(Method method : methods)
			{
				methodName = method.getName();
				if(methodName.startsWith(MyConstant.SET.getValue()))
				{
					String key = methodName.substring(3).toLowerCase();
					method.invoke(object, result.get(key));
				}
			}
		} catch (Exception e) {
			System.out.println("error");
		}
	}
}

enum MyConstant 
{
	SET("set"),GET("get");
	
	MyConstant(String value)
	{
		this.value = value;
	}
	
	private String value;
	

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}
呵呵 这是一个备忘录模式的代码刚敲好,希望对家多给点意见 谢谢。。。。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值