Java设计模式--原型模式

某些对象进行创建时可能需要较大的代价,但又需要许多重复对象时,可以利用这种模式去创建。这种模式主要是借助于Cloneable接口来实现的。

简单实现:

public class Product implements Cloneable{
    private String name ;
    private int id;
    private ArrayList<String> list = new ArrayList<>();

    public Product(String name,int id){
        System.out.println("构造函数");
        this.name = name;
        this.id = id;
    }

    public void addItem(String name){
        list.add(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public Product clone() {
        try {
            Product clone = (Product) super.clone();
            clone.id = this.id;
            clone.name = this.name;
            clone.list = this.list;
            return clone;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String toString() {
        return "name: " + name + " id: " + id + " list: " + list.toString();
    }
}

测试类:

public class test {
    public static void main(String[] args) {
        Product product = new Product("apple",101);
        product.addItem("item1");
        product.addItem("item2");
        Product clone = product.clone();
        clone.setId(102);
        System.out.println(product);
        System.out.println(clone);
    }
}

运行结果:

构造函数
name: apple id: 101 list: [item1, item2]
name: apple id: 102 list: [item1, item2]

可见调用clone并不会执行类的构造函数,而且对拷贝对象的修改不会影响原始对象。接下来我们来改变list的内容,看一下现象:

clone.addItem("item3");
System.out.println(product);
System.out.println(clone);

结果如下:

name: apple id: 101 list: [item1, item2, item3]
name: apple id: 102 list: [item1, item2, item3]

发现原始对象也随之被改变了,这样就违背了原形模式的初衷,这里涉及到深浅拷贝的问题。前面我们做的属于浅拷贝,这种拷贝对于引用类型的对象,只是简单的引用原始对象的字段,并没有进行完全的拷贝。要避免这种现象就要实现深拷贝。可以简单修改clone方法如下:

    @Override
    public Product clone() {
        try {
            Product clone = (Product) super.clone();
            clone.id = this.id;
            clone.name = this.name;
            clone.list = (ArrayList<String>) this.list.clone();
            return clone;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

这里我们在拷贝ArrayList时也调用了他自己的clone方法,继续运行测试类,结果如下:

name: apple id: 101 list: [item1, item2]
name: apple id: 102 list: [item1, item2, item3]

这次就达到我们的目的了。这里也就给我们提一个醒,尽量使用深拷贝,可以预防一些安全性问题,另外我们一些自己定义的类,在拷贝时,也要有自己的clone方法。

最后我们可以简单看一下ArrayList的cone实现:

public Object clone() {
        try {
            ArrayList<?> v = (ArrayList<?>) super.clone();
            v.elementData = Arrays.copyOf(elementData, size);
            v.modCount = 0;
            return v;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
    }

发现就是简单的想clone自己,然后clone整个数组.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 定义 原型模式属于对象的创建模式。通过给出一个原型对象来指明所有创建的对象的类型,然后用复制这个原型对象的办法创建...
    步积阅读 1,373评论 0 2
  • 原型模式,顾名思义就是对现有的一个对象进行复制克隆出一个全新的对象。被复制的对象就叫做原型对象,复制出来的克隆对象...
    MrKing5946阅读 343评论 0 0
  • 原型模式(Prototype Pattern) 什么是原型模式呢?其实就是使用原型实例指定创建对象的种类,并且通过...
    ghwaphon阅读 606评论 0 3
  • 今天去超市看到一个老人家买西红柿有的灵感,加上这个暑假听到的很多故事,就是这个模样了。不觉得写得多好,但是酣畅淋漓...
    唐子茵阅读 411评论 10 0
  • 最近,看到了吴军老师的几篇文章,都关于对命运的思考的,有一些特别的感觉,话题都是老生常谈,有无数人谈论过,可这次阅...
    温派尔的自传阅读 249评论 3 1