设计原则&模式:原型模式 Prototype(创建型)

定义:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。也就是说,这种不通过new关键字来产生一个对象,而是通过对象复制(Java中的clone或反序列化)来实现的模式,就叫做原型模式。

性能优良:原型模式是在内存二进制流的拷贝,要比直接new一个对象性能好很多,特别是要在一个循环内产生大量的对象时,原型模式可能更好地体现其优点。

逃避构造函数的约束,直接在内存中拷贝,构造函数是不会执行的。(Prototype.java)

浅拷贝与深拷贝实例。(Prototype1.java)

 

使用场景:

1、类的初始化需要消耗非常多的资源。

2、通过new产生一个对象需要非常繁琐的数据准备或访问。

3、一个对象需要提供给其他对象访问,而且每个调用者可能都需要修改其值时。

 

Prototype.java

public class Prototype implements Cloneable{
    public static void main(String[] args){
        Prototype prototype = new Prototype();
        /* 执行clone方法创建的Prototype对象 */
        Prototype clonePrototype = prototype.clone();
    }
    public Prototype(){
        System.out.println("-----Prototype的构造方法被执行了-----");
    }
    
    @Override
    protected Prototype clone(){
        try{
            return (Prototype)super.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return null;
    }
}

 

 

浅拷贝

@Data
public class Prototype1 implements Cloneable{
    private String name;
    private List<String> arrayList = new ArrayList<>();
    public static void main(String[] args){
        Prototype1 prototype1 = new Prototype1();
        prototype1.setName("orign object");
        prototype1.setValue("orign object");
        
        Prototype1 clonePrototype1 = prototype1.clone();
        clonePrototype1.setName("clone object");
        /* 发现添加了clone对象的setValue之后,也修改了prototype1中的arrayList中数据 */
        clonePrototype1.setValue("clone object");
        System.out.println(prototype1);
        System.out.println(clonePrototype1);
    }
    public void setValue(String value){this.arrayList.add(value);}
    public List<String> getValue(){return this.arrayList;}
    
    
    /**
     * 浅拷贝
     * @return
     */
    @Override
    protected Prototype1 clone(){
        try{
            return (Prototype1)super.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return null;
    }
     
}

 

 

深拷贝

@Data
public class Prototype1 implements Cloneable{
    private String name;
    private List<String> arrayList = new ArrayList<>();
    public static void main(String[] args){
        Prototype1 prototype1 = new Prototype1();
        prototype1.setName("orign object");
        prototype1.setValue("orign object");
        
        Prototype1 clonePrototype1 = prototype1.clone();
        clonePrototype1.setName("clone object");
        /* 发现添加了clone对象的setValue之后,也修改了prototype1中的arrayList中数据 */
        clonePrototype1.setValue("clone object");
        System.out.println(prototype1);
        System.out.println(clonePrototype1);
    }
    public void setValue(String value){this.arrayList.add(value);}
    public List<String> getValue(){return this.arrayList;}
    
    
    /**
     * 深拷贝
     * @return
     */
    @Override
    protected Prototype1 clone(){
        Prototype1 prototype1 = null;
        try{
            prototype1 = (Prototype1)super.clone();
            prototype1.setArrayList(new ArrayList<>());
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return prototype1;
    }
    
    
}

 

 

END

posted @ 2024-02-29 17:59  HOUHUILIN  阅读(2)  评论(0编辑  收藏  举报