享元模式
-  享元
 
-  享受,元数据
 
-  同一个数据,我就认为是一个元数据,整个系统里这个数据就一份,缓存起来
 
-  整个系统对这个数据,全部享受他一个对象实例即可
 
-  直接既有内存来缓存一块数据,用享元模式
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
   | import java.util.HashMap; import java.util.Map;
  public class FlyweightPatternDemo {          public static void main(String[] args) {         Flyweight flyweight1 = FlyweightFactory.get("对象1");          flyweight1.execute();                  Flyweight flyweight2 = FlyweightFactory.get("对象1");          flyweight2.execute();                  System.out.println(flyweight1 == flyweight2);       }
      public static interface Flyweight {                  void execute();         String getName();         void setName(String name);              }          public static class ConcreteFlyweight implements Flyweight {
          private String name;                  public String getName() {             return name;         }
          public void setName(String name) {             this.name = name;         }
          public ConcreteFlyweight(String name) {             super();             this.name = name;         }
          public void execute() {             System.out.println(name + "执行功能逻辑");         }              }          public static class FlyweightFactory {                  private static Map<String, Flyweight> cachePool = new HashMap<String, Flyweight>();                           public static Flyweight get(String name) {             Flyweight flyweight = cachePool.get(name);             if(flyweight == null) {                 flyweight = new ConcreteFlyweight(name);                 cachePool.put(name, flyweight);             }             return flyweight;         }              }      }
   | 
 
原型模式
-  原型模式,就是在要拷贝的类里实现一个clone()方法,自己拷贝自己
 
-  拷贝的时候,就两个概念,浅拷贝,深拷贝
 
-  很多地方要克隆这个对象,不要自己维护克隆的逻辑,即使克隆逻辑修改了,只要在clone()方法里面修改
 
在做业务的时候,我们有时为了隔离变化,会将DAO查询出来的Entity,和对外提供的DTO隔离开来。大概90%的时候,它们的结构都是类似的,但是我们很不喜欢写很多冗长的b.setF1(a.getF1())这样的代码,于是我们需要BeanCopier来帮助我们。选择Cglib的BeanCopier进行Bean拷贝的理由是,其性能要比Spring的BeanUtils,Apache的BeanUtils和PropertyUtils要好很多,尤其是数据量比较大的情况下。
BeanCopier工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
   | import java.util.HashMap; import java.util.Map;
  import net.sf.cglib.beans.BeanCopier;
 
 
 
 
 
  public class BeanCopierUtils {               
 
      public static Map<String, BeanCopier> beanCopierCacheMap = new HashMap<String, BeanCopier>();              
 
 
 
      public static void copyProperties(Object source, Object target){           String cacheKey = source.getClass().toString() +                  target.getClass().toString();                    BeanCopier beanCopier = null;                                      if (!beanCopierCacheMap.containsKey(cacheKey)) {                                         synchronized(BeanCopierUtils.class) {                                                    if(!beanCopierCacheMap.containsKey(cacheKey)) {                                             beanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);                        beanCopierCacheMap.put(cacheKey, beanCopier);                    } else {                      beanCopier = beanCopierCacheMap.get(cacheKey);                     }             }         } else {               beanCopier = beanCopierCacheMap.get(cacheKey);            }                    beanCopier.copy(source, target, null);       }      }  
 
   |