一起学设计模式 - 原型模式

原型模式(Prototype Pattern)是创建模式的一种,其作用是提高创建效率,减少计算机资源开销,与工厂模式类似的是,都屏蔽了对象实例化的过程…

<!– more –>

概述

原型模式23GOF模式的一种,其特点就是通过克隆/拷贝的方式来,节约创建成本和资源,被拷贝的对象模型就称之为原型

举例:在写PPT的时候,大多情况下模板风格都是一致的,只是其中部分描述内容发生变化,这个时候你会选择Ctrl+C/V还是新建一页PPT一边听《从头再来》一边调整图片和样式?

JAVA中对原型模式提供了良好的支持,我们只需要实现Cloneable接口即可,它的目的就是将对象标记为可被复制

优点

  • 简化对象创建过程,通过拷贝的方式构建效率更高
  • 可运行时指定动态创建的对象

缺点

  • 需要实现 Cloneable接口,clone位于内部,不易扩展,容易违背开闭原则(程序扩展,不应该修改原有代码)
  • 默认的 clone 只是浅克隆,深度克隆需要额外编码(比如:统一实现Cloneable接口,或者序列化方式,还有org.apache.commons:commons-lang3.SerializationUtils.java)

注意点

  • 通过内存拷贝的方式构建出来的,会忽略构造函数限制
  • 需要注意深拷贝浅拷贝,默认Cloneable浅拷贝,只拷贝当前对象而不会拷贝引用对象,除非自己实现深拷贝
  • 单例模式冲突,clone是直接通过内存拷贝的方式,绕过构造方法
  • 常用克隆不可变对象,如果你克隆的对象10个字段改9个还不如实例化算了
  • clone只是一个语法,非强制方法命名
  • 很少单独出现,常与工厂模式相伴

适用场景

  • 常用在初始化步骤繁琐,资源耗损严重的对象

案例

案例一:浅拷贝

class Address {
    private String description;
    // 省略 Getter And Setter 
}
public class Customer implements Cloneable {

    private int id;
    private String name;
    private Address address;
    private List<String> hobbies;

    // 省略 Getter And Setter 

    @Override
    protected Customer clone() throws CloneNotSupportedException {
        return (Customer) super.clone();
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address=" + address +
                ", hobbies=" + hobbies +
                ",  customer-hashCode=" + this.hashCode() +
                ",  address-hashCode=" + this.address.hashCode() +
                '}';
    }
}

public class PrototypeDemo {

    public static void main(String[] args) throws CloneNotSupportedException {
        List<String> hobbies = new ArrayList<>();
        hobbies.add("篮球");
        hobbies.add("足球");
        Customer original = new Customer(1, "淘宝客户-1", new Address("上海市"), hobbies);
        Customer cloned = original.clone();
        System.out.println("////////////////////////////////////////////////// 修改前原始信息信息 - 开始 //////////////////////////////////////////////////");
        System.out.println("原始信息:" + original.toString());
        System.out.println("拷贝信息:" + cloned.toString());
        System.out.println("////////////////////////////////////////////////// 修改前原始信息信息 - 结束 //////////////////////////////////////////////////\n\n");
    }
}

分析: 从日志中可以发现,我们无需通过new Object()的方式去实例化对象,而是调用Object.clone()同样可以,CustomerhashCode也发生了改变,由此可以推断出它们的引用已经发生变化了,但是AddresshashCode一模一样,前面说到过Cloneable浅克隆的,并不会拷贝其它引用对象

////////////////////////////////////////////////// 修改前原始信息信息 - 开始 //////////////////////////////////////////////////
原始信息:Customer{id=1, name='淘宝客户-1', address=Address{description='上海市'}, hobbies=[篮球, 足球],  customer-hashCode=460141958,  address-hashCode=1163157884}
拷贝信息:Customer{id=1, name='淘宝客户-1', address=Address{description='上海市'}, hobbies=[篮球, 足球],  customer-hashCode=1956725890,  address-hashCode=1163157884}
////////////////////////////////////////////////// 修改前原始信息信息 - 结束 //////////////////////////////////////////////////

案例二:浅拷贝

public class PrototypeDemo {

    public static void main(String[] args) throws CloneNotSupportedException {
        List<String> hobbies = new ArrayList<>();
        hobbies.add("篮球");
        hobbies.add("足球");
        Customer original = new Customer(1, "淘宝客户-1", new Address("上海市"), hobbies);
        Customer cloned = original.clone();

        System.out.println("////////////////////////////////////////////////// 修改后原始信息信息 - 开始 //////////////////////////////////////////////////");
        original.setName("淘宝客户-2");
        original.getHobbies().add("音乐");
        System.out.println("原始信息:" + original.toString());
        System.out.println("拷贝信息:" + cloned.toString());
        System.out.println("////////////////////////////////////////////////// 修改后原始信息信息 - 结束 //////////////////////////////////////////////////\n\n");
    }
}

分析: Customer本身的属性值修改与原始对象并不冲突,它们都是各自一份,但集合类型修改后,克隆对象输出的与原始对象如出一辙,不难发现Cloneable浅克隆范围只支持基本类型

////////////////////////////////////////////////// 修改后原始信息信息 - 开始 //////////////////////////////////////////////////
原始信息:Customer{id=1, name='淘宝客户-2', address=Address{description='上海市'}, hobbies=[篮球, 足球, 音乐],  customer-hashCode=460141958,  address-hashCode=1163157884}
拷贝信息:Customer{id=1, name='淘宝客户-1', address=Address{description='上海市'}, hobbies=[篮球, 足球, 音乐],  customer-hashCode=1956725890,  address-hashCode=1163157884}
////////////////////////////////////////////////// 修改后原始信息信息 - 结束 //////////////////////////////////////////////////

案例三:深浅拷贝命名?

class Address implements Cloneable {
    // Address 也实现 Cloneable
}

@Override
protected Address clone() throws CloneNotSupportedException {
    return (Address) super.clone();
}


public class Customer implements Cloneable {

    // 重复代码省略...

    protected Customer shallow() throws CloneNotSupportedException {
        return (Customer) super.clone();
    }

    protected Customer deep() throws CloneNotSupportedException {
        Customer clone = (Customer) super.clone();
        clone.setAddress(clone.getAddress().clone());
        return clone;
    }
}

public class PrototypeDemo {

    public static void main(String[] args) throws CloneNotSupportedException {

        System.out.println("////////////////////////////////////////////////// 半深浅克隆 - 开始 //////////////////////////////////////////////////");
        original.setAddress(new Address("北京市"));
        Customer shallow = original.shallow();
        System.out.println("浅克隆:" + shallow.toString());

        Customer deep = original.deep();
        original.setAddress(new Address("天津市"));
        original.getHobbies().add("电影");
        System.out.println("深克隆:" + deep.toString());
        System.out.println("////////////////////////////////////////////////// 半深浅克隆 - 结束 //////////////////////////////////////////////////\n\n");
    }
}

分析: 我们定义了两个非clone的方法名,同样可以做到克隆特性,此时从deep()中可以看到修改原始的Address并没有影响到现有的克隆对象,这是因为内部通过硬编码的方式控制的,虽然引用的Address对象发生改变,但是List<String>还是同一个引用,依旧做不到完全的深度克隆

////////////////////////////////////////////////// 半深浅克隆 - 开始 //////////////////////////////////////////////////
浅克隆:Customer{id=1, name='淘宝客户-2', address=Address{description='北京市'}, hobbies=[篮球, 足球, 音乐],  customer-hashCode=356573597,  address-hashCode=1735600054}
深克隆:Customer{id=1, name='淘宝客户-2', address=Address{description='北京市'}, hobbies=[篮球, 足球, 音乐, 电影],  customer-hashCode=21685669,  address-hashCode=2133927002}
////////////////////////////////////////////////// 半深浅克隆 - 结束 //////////////////////////////////////////////////

案例四:序列化实现深度克隆

class Address implements Cloneable,Serializable {

    private static final long serialVersionUID = 783202091017893997L;

}
public class Customer implements Cloneable, Serializable {
    private static final long serialVersionUID = 783202091017893997L;

    protected Customer deepCopy() throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(this);
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bis);
        return (Customer) in.readObject();
    }
}

public class PrototypeDemo {

    public static void main(String[] args) throws CloneNotSupportedException {

        System.out.println("////////////////////////////////////////////////// 深浅克隆 - 开始 //////////////////////////////////////////////////");
        Customer deepCopy = original.deepCopy();
        original.setAddress(new Address("长沙市"));
        original.getHobbies().add("乒乓球");
        System.out.println("深克隆:" + deepCopy.toString());

        System.out.println("////////////////////////////////////////////////// 深浅克隆 - 结束 //////////////////////////////////////////////////\n\n");
    }
}

分析: 实现Serializable,通过序列化的方式与原始数据完全脱离关系,从而达到深度克隆效果,当然一般用SerializationUtils.clone(original)方式比我们自己写的会更好

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>
////////////////////////////////////////////////// 深浅克隆 - 开始 //////////////////////////////////////////////////
深克隆:Customer{id=1, name='淘宝客户-2', address=Address{description='天津市'}, hobbies=[篮球, 足球, 音乐, 电影],  customer-hashCode=1020371697,  address-hashCode=789451787}
////////////////////////////////////////////////// 深浅克隆 - 结束 //////////////////////////////////////////////////

案例五:如何配合工厂模式使用?

这个简单问题就留给读者思考吧,欢迎探讨交流,三人行必有我师!!!

总结

本章介绍了什么是原型模式以及使用它的优缺点,其模式在开发过程中几乎用不上(反正我没用到过,欢迎探讨),但这并不能说明它是无用,存在即合理,设计模式不仅用作与JAVA软件编程,更多情况下模式的作用是在某种程度下避免复杂的设计

– 说点什么

全文代码:https://gitee.com/battcn/design-pattern/tree/master/Chapter3/battcn-prototype

  • 个人QQ:1837307557
  • battcn开源群(适合新手):391619659

微信公众号:battcn(欢迎调戏)

    原文作者:设计模式
    原文地址: https://segmentfault.com/a/1190000011762181
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞