(行为型模式九)备忘录模式
来源:互联网 发布:安知玉如意说的是什么 编辑:程序博客网 时间:2024/06/06 16:26
package com.eyugame.modle;import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;/** * 备忘录模式 * @author JYC506 * *//*备份类*/public class Memento {Map<String, Map<String,Object>> map = new HashMap<String, Map<String,Object>>();public void put(String key,Map<String,Object> proMap){this.map.put(key, proMap);}public void restoreBean(Object bean,String key){BeanUtil.restoreBean(bean, map.get(key));}public static void main(String[] args) {Phone phone=new Phone("小米note",2300,"白色");System.out.println("没修改前的:"+phone);/*备份*/Memento memento=new Memento();memento.put("01", BeanUtil.toMap(phone));/*修改*/phone.setColor("黑色");phone.setPrice(1999);System.out.println("修改后:"+phone); /*回滚*/memento.restoreBean(phone, "01");System.out.println("回滚后:"+phone);}}class BeanUtil {/* 把对象的属性存贮起来 */public static Map<String, Object> toMap(Object bean) {Map<String, Object> map = new HashMap<String, Object>();try {BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();for (PropertyDescriptor pd : propertyDescriptors) {String name = pd.getName();Method method = pd.getReadMethod();if(!name.equalsIgnoreCase("class")){Object value = method.invoke(bean, new Object[] {});map.put(name, value);}}} catch (Exception e) {e.printStackTrace();}return map;}/* 根据对象属性存贮库恢复对象属性 */public static void restoreBean(Object bean, Map<String, Object> proMap) {try {BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();for (PropertyDescriptor pd : propertyDescriptors) {String name = pd.getName();Method method = pd.getWriteMethod();if(!name.equalsIgnoreCase("class")){ method.invoke(bean, proMap.get(name));}}} catch (Exception e) {e.printStackTrace();}}}/*手机*/class Phone {private String name;private double price;private String color;public Phone(String name, double price, String color) {super();this.name = name;this.price = price;this.color = color;}@Overridepublic String toString() {return "名称:"+this.name+" 价格:"+this.price+" 颜色:"+this.color;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}}
0 0
- (行为型模式九)备忘录模式
- 备忘录模式(行为型)
- 备忘录模式(行为型)
- 行为型模式-----备忘录模式(Memento)
- 行为型模式--备忘录模式
- 行为型模式-备忘录模式
- 十八、备忘录模式Memento(行为型)
- 备忘录模式Memento(对象行为型)
- Java设计模式之行为型模式(备忘录模式)
- 设计模式 - 行为型模式 - 备忘录模式
- 设计模式->行为型模式->备忘录模式
- 行为模式--备忘录模式
- 行为模式-备忘录模式
- 备忘录模式 - 行为模式
- 行为型模式-备忘录memento
- 行为型之备忘录模式
- 行为模式:Memento(备忘录)
- 设计模式(4)-行为型-备忘录模式(Memento)
- gdb 如何调试release版本
- Android中跨进程通信传递Parcelable对象时出现android.os.BadParcelableException: ClassNotFoundException when unmarsh
- hdu 1870 愚人节的礼物
- nyoj-915-+-字符串
- EMU8086 简单汇编程序&实现5个BYTES的数据相加
- (行为型模式九)备忘录模式
- C++中new、delete 与new[]、delete[]
- win8下安装pip
- 欢迎使用CSDN-markdown编辑器
- 单链表模板
- coco2dx-动作(Action)原理二 动作分类详解
- POJ1463 Strategic game(树形DP)
- java之类对象的加载
- Java使用简单工厂模式对面向接口编程模式的深度解耦实现