Android设计模式-原型模式

2019-10-18  本文已影响0人  星宇V

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图

原型模式UML图

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

3.具体实现

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

3.2具体原型类

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

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

    public int getI() {
        return i;
    }

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

    public ArrayList<String> 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.总结

 @Override
    public TestPrototype clone() throws CloneNotSupportedException {
        TestPrototype prototype=null;
        prototype= (TestPrototype) super.clone();
        prototype.list= (ArrayList<String>) this.list.clone();
        System.out.println("执行了复制");
        return prototype;
    }

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

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

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

上一篇 下一篇

猜你喜欢

热点阅读