【23种设计模式】之备忘录模式(memento pattern)

说明

  • 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的对象。

角色

  • 备忘录角色:存储状态
  • 发起人角色:创建备忘录,并利用备忘录存储自己的状态
  • 负责人:管理备忘录
  • 客户端

代码实现

  • 通过map来记录对象的值
public class BackUp {
    private Map<String,Object> objectMap;
    BackUp(Map<String,Object> objectMap){
        this.objectMap = objectMap;
    }

    public Map<String, Object> getObjectMap() {
        return objectMap;
    }

    public void setObjectMap(Map<String, Object> objectMap) {
        this.objectMap = objectMap;
    }
}
public class BeanUtils {

    public static Map<String,Object> createBackUp(Object object){
        Map<String,Object> result = new HashMap<>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
            PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
            for(PropertyDescriptor propertyDescriptor:descriptors){
                String fieldName = propertyDescriptor.getName();
                Method getter = propertyDescriptor.getReadMethod();
                Object fieldValue = getter.invoke(object,new Object[]{});
                if(!fieldName.equals("class")){
                    result.put(fieldName,fieldValue);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    public static void recover(Object object,Map<String,Object> objectMap){
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
            PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
            for(PropertyDescriptor propertyDescriptor:descriptors){
                String fieldName = propertyDescriptor.getName();
                if(objectMap.containsKey(fieldName)){
                    Method setter = propertyDescriptor.getWriteMethod();
                    setter.invoke(object,new Object[]{objectMap.get(fieldName)});
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
public class Manager {
    private Map<String,BackUp> backUpMap = new HashMap<>();

    public void setBackUp(String key,BackUp backUp){
        backUpMap.put(key,backUp);
    }
    public BackUp getBackUp(String key){
        return backUpMap.get(key);
    }
}
public class User {
    private String state1;
    private String state2;
    private String state3;

    public String getState1() {
        return state1;
    }

    public void setState1(String state1) {
        this.state1 = state1;
    }

    public String getState2() {
        return state2;
    }

    public void setState2(String state2) {
        this.state2 = state2;
    }

    public String getState3() {
        return state3;
    }

    public void setState3(String state3) {
        this.state3 = state3;
    }
    public BackUp createBackup(){
        return new BackUp(BeanUtils.createBackUp(this));
    }
    public void recover(BackUp backUp){
        BeanUtils.recover(this,backUp.getObjectMap());
    }
    @Override
    public String  toString(){
        return "打印:"+state1+","+state2+","+state3;
    }
}
public class Test {
    public static void main(String[] args){
        Manager manager = new Manager();
        User user = new User();
        user.setState1("1");
        user.setState2("2");
        user.setState3("3");
        System.out.println(user.toString());
        manager.setBackUp("0001",user.createBackup());
        user.setState1("5555");
        System.out.println(user.toString());
        user.recover(manager.getBackUp("0001"));
        System.out.println(user.toString());
    }
}

你可能感兴趣的:(#,设计模式,设计模式,备忘录模式,java)