Clone a single tone object

Here is the listing below:


public class Car {
        private static Car car = null;

        private void car() {
        }

        public static Car GetInstance() {
            if (car == null) {
                car = new Car();
            }
            return car;
        }

        public static void main(String arg[]) throws CloneNotSupportedException {
            car = Car.GetInstance();
            Car car1 = (Car) car.clone();
            System.out.println(car.hashCode());// getting the hash code
            System.out.println(car1.hashCode());
        }
    }

Why do this code throw cloneNotSupportedException?


public class Car implements Cloneable {
    private static Car car = null;

    public static Car GetInstance() {
        if (car == null) {
            car = new Car();
        }
        return car;
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

Car car = Car.GetInstance();
Car car1 = (Car) car.clone();
System.out.println(car.hashCode());
System.out.println(car1.hashCode());

输出:

1481395006
2027946171

If you are cloning the singleton object then you are violating the Design Principle of Singleton.

By default clone method is protected: protected native Object clone() throws CloneNotSupportedException ;

If your Car extends another class that does support cloning, it is possible to violate the design principles of the singleton. So, to be absolutely positively 100% certain that a singleton really is a singleton, we must add a clone() method of our own, and throw a CloneNotSupportedException if anyone try to create. Below is the our override clone method.

 @Override
    protected Object clone() throws CloneNotSupportedException {
        /*
         * Here forcibly throws the exception for preventing to be cloned
         */
        throw new CloneNotSupportedException();
        // return super.clone();
    }

Please find the below code block to work Clone for Singleton class or avoid the cloning by uncommenting the code.

public class Car  implements Cloneable {

    private static Car car = null;

    private void Car() {
    }

    public static Car GetInstance() {
        if (car == null) {
            synchronized (Car.class) {
                   if (car == null) {
                car = new Car();
                   }
            }
        }
        return car;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        /*
         * Here forcibly throws the exception for preventing to be cloned
         */
     //   throw new CloneNotSupportedException();
        return super.clone();
    }

    public static void main(String arg[]) throws CloneNotSupportedException {
        car = Car.GetInstance();
        Car car1 = (Car) car.clone();
        System.out.println(car.hashCode());// getting the hash code
        System.out.println(car1.hashCode());
    }
}    
链接地址: http://www.djcxy.com/p/49630.html

上一篇: java中主要方法中String args []的用途

下一篇: 克隆单个音色对象