睡觉前就再讲一下备忘录模式吧呵呵。。
备忘录模式所要完成的功能就是能做一个备忘的功能,可以把一个对象还原到这个对象上个状态的功能。说白了就是能向我们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;
}
}
呵呵 这是一个备忘录模式的代码刚敲好,希望对家多给点意见 谢谢。。。。