栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

学习记录之原型模式(Prototype)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学习记录之原型模式(Prototype)

目录

1.Prototype介绍2.引例3.Java重写Clone()方法实现prototype4.使用序列化实现prototype的深拷贝5.总结

1.Prototype介绍

释义:用原型实例指定创建对象的种类,通过拷贝这些原型,创建新的对象;prototype是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的细节;单例模式是仅可创建一个实例,原型模式可以创建多个实例。 2.引例

现有一只羊(Sheep类)有如下属性:String name = 喜羊羊,Integer age = 6666,Sheep friend = new Sheep(“懒羊羊”, “5”);如何创建与上面一只羊属性完全相同的5只羊?

3.Java重写Clone()方法实现prototype

Sheep类

public class Sheep implements Serializable, Cloneable {
    private String name;
    private Integer age;
    private Sheep friend;

    public Sheep(String name, Integer age, Sheep friend) {
        this.name = name;
        this.age = age;
        this.friend = friend; // 对象,clone()默认是浅拷贝
    }

    @Override
    protected Object clone(){
        Sheep sheep = null;

        try {
            sheep = (Sheep) super.clone(); // 默认浅拷贝
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return sheep;
    }
......略

注意默认的clone()方法是浅拷贝的:

public static void main(String[] args) throws CloneNotSupportedException {
        Sheep sheepOne = new Sheep("喜羊羊", 6666, new Sheep("懒羊羊", 5, null));

        ArrayList sheepArrayList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Sheep sheep = (Sheep) sheepOne.clone();
            sheepArrayList.add(sheep);
        }

        for (Sheep sheep : sheepArrayList) {
            System.out.println(sheep+"self hashcode:"+sheep.hashCode()+"-->friend hashcode:"+sheep.getFriend().hashCode());
        }
    }
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:460141958-->friend hashcode:1163157884
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:1956725890-->friend hashcode:1163157884
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:356573597-->friend hashcode:1163157884
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:1735600054-->friend hashcode:1163157884
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:21685669-->friend hashcode:1163157884

可以看出clone()虽然创建了5个新Sheep实例,但5个新Sheep中的friend成员依然是同一个对象,这是因为clone()默认是浅拷贝,如果进行深拷贝需要重写clone()方法或者使用序列化来实现。

重写clone()方法:

@Override
    protected Object clone() {
        Sheep sheep = null;

        try {
            //基本数据类型 包装器类型 String 的克隆
            sheep = (Sheep) super.clone();
            // 引用类型的属性单独处理
            if (sheep.friend != null) {
                sheep.friend = (Sheep) sheep.friend.clone();
            }
        } catch (Exception e) {
            System.out.println("exception: " + e.getMessage());
        }

        return sheep;
    }
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:460141958-->friend hashcode:1163157884
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:1956725890-->friend hashcode:356573597
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:1735600054-->friend hashcode:21685669
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:2133927002-->friend hashcode:1836019240
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:325040804-->friend hashcode:1173230247
4.使用序列化实现prototype的深拷贝
    public Object serializableClone() {
        Sheep sheep = null;
        // 创建流对象
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;

        try {
            // 序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);// 当前这个对象以对象流的方式输出

            // 反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);

            sheep = (Sheep) ois.readObject();
            return sheep;
        } catch (Exception e) {
            System.out.println("exception:" + e.getMessage());
            return null;
        } finally {
            try {
                ois.close();
                bis.close();
                oos.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
public static void main(String[] args) throws CloneNotSupportedException {
        Sheep sheepOne = new Sheep("喜羊羊", 6666, new Sheep("懒羊羊", 5, null));

        ArrayList sheepArrayList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Sheep sheep = (Sheep) sheepOne.serializableClone();
            sheepArrayList.add(sheep);
        }

        for (Sheep sheep : sheepArrayList) {
            System.out.println(sheep+"self hashcode:"+sheep.hashCode()+"-->friend hashcode:"+sheep.getFriend().hashCode());
        }
    }

Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:1915910607-->friend hashcode:284720968
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:189568618-->friend hashcode:793589513
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:1313922862-->friend hashcode:495053715
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:1922154895-->friend hashcode:883049899
Sheep{name='喜羊羊', age=6666, friend=Sheep{name='懒羊羊', age=5, friend=null}}self hashcode:2093176254-->friend hashcode:1854731462
5.总结

创建新对象比较复杂时,可以利用原型模式简化对象的创建过程,同时也能提升效率;不用重新初始化对象,而是动态的获得对象运行时的状态;如果原始对象发生变化(增加或减少属性),其他克隆对象也会发生相应的变化,无需修改代码;在需要进行深克隆的时候需要重写clone方法或者进行序列化与反序列化缺点:需要为每一个配备一个克隆方法,尤其对已有的类进行改造时,需要更改源代码,违背了ocp(开闭)原则。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/782173.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号