zoukankan      html  css  js  c++  java
  • 设计模式——原型模式

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

    阅读前准备

    • 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设计模式案例

    声明

    引用该文档请注明出处

  • 相关阅读:
    poj 1984 Navigation Nightmare(带权并查集+小小的技巧)
    zoj 3261 Connections in Galaxy War(并查集逆向加边)
    poj 1733 Parity game(带权并查集)
    poj 1456 Supermarket(贪心+优先队列)
    hdu 3038 How Many Answers Are Wrong(并查集的思想利用)
    poj 1182 食物链(种类并查集 ‘初心者’)
    hdu 1182 A Bug's Life(简单种类并查集)
    hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)
    CodeForces 779D. String Game(二分答案)
    poj 3169 Layout(差分约束+spfa)
  • 原文地址:https://www.cnblogs.com/imaikce/p/10903157.html
Copyright © 2011-2022 走看看