Android设计模式-原型模式

UML关系简单介绍
UML简单使用的介绍
创建型设计模式
Android设计模式-单例模式
Android设计模式-工厂模式
Android设计模式-抽象工厂模式
Android设计模式-建造者模式
Android设计模式-原型模式
结构型设计模式
Android设计模式-代理模式
Android设计模式-装饰模式
Android设计模式-适配器模式
Android设计模式-组合模式
Android设计模式-门面模式
Android设计模式-桥接模式
Android设计模式-享元模式
行为型设计模式
Android设计模式-策略模式
Android设计模式-命令模式
Android设计模式-责任链模式
Android设计模式-模版方法模式
Android设计模式-迭代器模式
Android设计模式-观察者模式
Android设计模式-备忘录模式
Android设计模式-中介者模式
Android设计模式-访问者模式
Android设计模式-状态模式
Android设计模式-解释器模式

1.定义

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

2.原型模式UML图

Android设计模式-原型模式_第1张图片
原型模式UML图

原型模式的核心就在于clone()这个方法,这个方法是在Object类下,java中所有类的根源。。。使用原型模式的时候,需要用 Cloneable接口来进行标记。

3.具体实现

3.1 抽象原型类一般不需要写

3.2具体原型类

public class TestPrototype implements Cloneable {
    private int i;
    private ArrayList list=new ArrayList<>();

    public TestPrototype() {
        System.out.println("执行了构造函数");
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    public ArrayList getList() {
        return list;
    }

    public void addList(String s) {
        list.add(s);
    }

    /**
     * 同一个包下,可以使用protected,跨包则改为public
     * @return
     * @throws
     */
    @Override
    public Object clone() throws CloneNotSupportedException {
        System.out.println("执行了复制");
        return super.clone();
    }
}

同一个包下,可以使用protected,跨包则改为public

3.3场景类调用

public class MyClass {
    public static void main(String args[]) {
        TestPrototype testPrototype=new TestPrototype();
        testPrototype.setI(10);
        testPrototype.addList("111");
        System.out.println("testPrototype对象中list为"+testPrototype.getList());
        TestPrototype testClone=null;
        try {
             testClone= (TestPrototype) testPrototype.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        if (null!=testClone){
            testClone.addList("222");
            System.out.println("复制的对象中i为"+testClone.getI());
            System.out.println("复制的对象中list为"+testClone.getList());
            System.out.println("testPrototype对象中list为"+testPrototype.getList());
            testClone.setI(20);
            System.out.println("testPrototype对象中i为"+testPrototype.getI());
        }
    }
}

3.4打印结果

执行了构造函数
testPrototype对象中list为[111]
执行了复制
复制的对象中i为10
复制的对象中list为[111, 222]
testPrototype对象中list为[111, 222]
testPrototype对象中i为10

4.总结

  • 从上图的打印结果中,可以看出,通过new创建的对象,会走他的构造函数,而通过clone创建的对象,是不会走构造函数的。
    具体原因是因为Object类的clone方法的原理是从堆内存中以二进制流的方式进行拷贝,重新分配一个内存块
  • 修改复制后对象的i值,原对象中的值不变,而修改复制后对象的list值,原对象中的值也发生了改变。
    原因在于深拷贝和浅拷贝前面的例子就是一个浅拷贝,两个对象共享了list这个变量。可能存在这个问题的变量比如数组,引用对象,其他基本数据类型加上string是不会有这种问题的。
    将上面的例子改动几行代码,就能实现深拷贝
    重写具体原型类中的clone方法为
 @Override
    public TestPrototype clone() throws CloneNotSupportedException {
        TestPrototype prototype=null;
        prototype= (TestPrototype) super.clone();
        prototype.list= (ArrayList) this.list.clone();
        System.out.println("执行了复制");
        return prototype;
    }

此时即可防止出现浅拷贝的问题。还是刚才的代码,打印数据,结果为

执行了构造函数
testPrototype对象中list为[111]
执行了复制
复制的对象中i为10
复制的对象中list为[111, 222]
testPrototype对象中list为[111]
testPrototype对象中i为10

深拷贝还有另一种实现方式就是通过自己写二进制流来操作对象,然后实现对象的深拷贝。
注意:深拷贝和浅拷贝建议不要混合使用,特别是涉及类的继承的时候,父类有多个引用的情况就非常复杂,建议的方案是深拷贝和浅拷贝分开实现。

  • 要使用clone方法,类的成员变量上不要添加final关键字,这俩是有冲突的。

你可能感兴趣的:(Android设计模式-原型模式)