23种设计模式(4) - 原型模式

1、定义

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

2、UML图

3、组成

  • 原型类:用来作拷贝的类,实现了Cloneable类,并重写了 clone方法。重写Cloneable方法才能使用clone方法,否则会报CloneNotSupportedException的异常;重写clone方法是因为该方法原来是protected类型的,不重写不能调用该方法。

4、代码

// 原型类
public class Prototype implements Cloneable {
    private String name;
  	// 引用类型变量
    private Car car;

    @Override
    protected Prototype clone() throws CloneNotSupportedException {
        Prototype prototype = (Prototype) super.clone();
        // 引用类型需要深复制
        prototype.car = (Car) car.clone();
        return prototype;
    }

    public Prototype(String name, Car car) {
        this.name = name;
        this.car = car;
    }

    // get、set方法省略 ...

    public void show(){
        System.out.println("我的名字:" + name  + ":::::: 车的名字:" + car.getName());
    }
}

public class Car implements Cloneable{
    private String name;

    public Car(String name) {
        this.name = name;
    }

  	// get、set方法省略 ...
   
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

// 引用类
public class Main {
    public static void main(String[] args) throws CloneNotSupportedException {
        Prototype prototype = new Prototype("原型1", new Car("原型1的CAR"));
        Prototype prototype2 = prototype.clone();
      	// 修改引用类的属性,看是否会影响第一个类
        prototype2.getCar().setName("原型2的CAR");

        prototype.show();
        prototype2.show();
    }
}

运行结果:

我的名字:原型1:::::: 车的名字:原型1的CAR
我的名字:原型1:::::: 车的名字:原型2的CAR

5、优缺点

  • 使用原型模式比直接new一个对象的性能要好的多,因为clone方法是Java本地方法,它直接操作二进制流来创建对象,特别是复制大对象时,性能相差较明显。
  • 需要注意,原型模式是直接在内存中操作二进制数据流的,不会去执行构造方法,而且类变量的访问权限级别也能被忽略。
  • 深拷贝与浅拷贝:Object类的clone方法只会拷贝对象中的基本的数据类型(基本类型及封装类型,String类型也会被拷贝),对于数组、容器对象、引用对象等都不会拷贝,只会复制对象的引用,这就是浅拷贝。如果要实现深拷贝,必须将原型模式中的数组、容器对象、引用对象等另行拷贝。

6、适用场景

如果项目中要频繁复制新对象,或是被复制对象比较复杂,可以考虑原型模式。

posted @ 2020-03-25 00:05  MoonGeek  阅读(351)  评论(2编辑  收藏  举报