原型模式_通过复制生成实例(避免实例重复创建从而减少内存消耗)

阅读前准备

  • 1、浅克隆(shallow clone),浅拷贝是指拷贝对象时仅仅拷贝对象本身和对象中的基本变量,而不拷贝对象包含的引用指向的对象。
    (如:对象A1中包含对B1的引用,B1中包含对C1的引用。浅拷贝A1得到A2,A2中依然包含对B1的引用,
    B1中依然包含对C1的引用。深拷贝则是对浅拷贝的递归,深拷贝A1得到A2,A2中包含对B2(B1的copy)的引用,B2中包含对C2(C1的copy)的引用)
  • 2、深克隆(deep clone),深拷贝不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象
    (需要重写clone方法.如
    @Override
          protected Object clone() throws CloneNotSupportedException {
            Husband husband = (Husband) super.clone();
              husband.wife = (Wife) husband.getWife().clone();
            return husband;
         }
      )

/**
 * 产品生成管理器
 * @author maikec
 * @date 2019/5/11
 */
public final class ProductManager {
    private final Map<String, Product> productMap = Collections.synchronizedMap(new HashMap<>(  ));
    public void register(Product product){
        productMap.put( product.getClass().getSimpleName(),product );
    }
    public Product create(Product product){
        Product result = productMap.get( product.getClass().getSimpleName() );
        if(null == result){
            register( product );
            result = productMap.get( product.getClass().getSimpleName() );
        }
        return result.createClone();
    }
}

/**
 * 原型类
 * @author maikec
 * @date 2019/5/11
 */
public interface Product extends Cloneable {
    void use();

    /**
     * 克隆
     * @return
     */
    Product createClone();
}

/**
 * @author maikec
 * @date 2019/5/11
 */
public class CloneFailureException extends RuntimeException {
    public CloneFailureException(){
        super("clone failure");
    }
    public CloneFailureException(String msg){
        super(msg);
    }
}

/**
 * @author maikec
 * @date 2019/5/11
 */
public class MessageProduct implements Product {
    @Override
    public void use() {
        System.out.println( "MessageProduct" );
    }

    @Override
    public MessageProduct createClone() {
        try {
            return (MessageProduct) clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            throw new CloneFailureException(  );
        }
    }
}

/**
 * @author maikec
 * @date 2019/5/11
 */
public class UnderlineProduct implements Product {
    @Override
    public void use() {
        System.out.println( "UnderlineProduct" );
    }

    @Override
    public UnderlineProduct createClone() {
        try {
            return (UnderlineProduct) clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            throw new CloneFailureException();
        }
    }
}

/**
 * @author maikec
 * @date 2019/5/11
 */
public class PrototypeDemo {
    public static void main(String[] args) {
        ProductManager manager = new ProductManager();
        manager.register( new UnderlineProduct() );
        manager.register( new MessageProduct() );

        manager.create(  new UnderlineProduct()  ).use();
        manager.create(  new MessageProduct()  ).use();
    }
}

 

附录

github.com/maikec/patt… 个人GitHub设计模式案例

声明

引用该文档请注明出处

posted on 2019-05-21 23:17  maikec陈  阅读(216)  评论(0编辑  收藏  举报