设为首页 加入收藏

TOP

悟空模式-java-原型模式(二)
2017-10-10 12:31:26 】 浏览:7787
Tags:悟空 模式 -java- 原型
being cloned and replacing the references to these * objects with references to the copies. If a class contains only * primitive fields or references to immutable objects, then it is usually * the case that no fields in the object returned by {
@code super.clone} * need to be modified. * <p> * The method {@code clone} for class {@code Object} performs a * specific cloning operation. First, if the class of this object does * not implement the interface {@code Cloneable}, then a * {@code CloneNotSupportedException} is thrown. Note that all arrays * are considered to implement the interface {@code Cloneable} and that * the return type of the {@code clone} method of an array type {@code T[]} * is {@code T[]} where T is any reference or primitive type. * Otherwise, this method creates a new instance of the class of this * object and initializes all its fields with exactly the contents of * the corresponding fields of this object, as if by assignment; the * contents of the fields are not themselves cloned. Thus, this method * performs a "shallow copy" of this object, not a "deep copy" operation. * <p> * The class {@code Object} does not itself implement the interface * {@code Cloneable}, so calling the {@code clone} method on an object * whose class is {@code Object} will result in throwing an * exception at run time. * * @return a clone of this instance. * @throws CloneNotSupportedException if the object's class does not * support the {@code Cloneable} interface. Subclasses * that override the {@code clone} method can also * throw this exception to indicate that an instance cannot * be cloned. * @see java.lang.Cloneable */ protected native Object clone() throws CloneNotSupportedException;

我们看到clone()方法是一个native方法,native方法的效率一般远高于非native方法。同时我们也可以看到关于clone()方法的描述也印证了Cloneable接口的相关介绍,如protected以及CloneNotSupportedException等。

关于clone()方法的表现如下:

x.clone() !=x;

x.clone().getClass() == x.getClass();

x.clone().equals(x) == true;

这里还要介绍关于深复制与浅复制的概念:

浅复制对象的所有属性都与原对象具有相同的值,包括引用其他对象的变量,对这些对象的引用依然指向原来的对象。

而深复制对象会将原对象的所有属性都复制一遍,包括原对象引用的对象,深复制会复制新的引用对象作为自己的变量而不使用原来的对象。

 

浅复制原型模式

package com.tirion.design.prototype;

public class GoldenCudgel implements Cloneable {

    public GoldenCudgel() {

    }

    public GoldenCudgel(boolean disappear) {
        this.disappear = disappear;
    }

    private boolean disappear;

    public boolean isDisappear() {
        return disappear;
    }

    public void setDisappear(boolean disappear) {
        this.disappear = disappear;
    }

    public GoldenCudgel clone() {
        GoldenCudgel goldenCudgel = null;
        try {
            goldenCudgel = (GoldenCudgel) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return goldenCudgel;
    }

    public boolean equals(GoldenCudgel obj) {
        return obj.isDisappear() == disappear;
    }

}

悟空

package com.tirion.design.prototype;

public class WuKong {

    private static GoldenCudgel goldenCudgel = new GoldenCudgel(false);

    public static void main(String[] args) {
        GoldenCudgel copyGoldenCudgel = goldenCudgel.clone();
        System.out.println(goldenCudgel);
        System.out.println(copyGoldenCudgel);
        System.out.println(goldenCudgel != copyGoldenCudgel);
        System.out.println(goldenCudgel.getClass() == copyGoldenCudgel.getClass());
        System.out.println(goldenCudgel.equals(copyGoldenCudgel));
    }

}

打印结果

com.tirion.design.prototype.GoldenCudgel@74a14482
com.tirion.
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇简易RPC框架-过滤器机制 下一篇设计模式精要

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目