java设计模式——创建者模式——原型模式

创建者模式——原型模式

原型模式概述:

原型模式的用意是创建一个实例作为“原型”,通过复刻这个“原型对象”的方式来创建一个和原型对象相同的对象。

原型模式结构:

原型模式包括如下角色:

  • 抽象原型类:规定了具体原型对象必须实现的clone方法。
  • 具体原型类:实现了抽象原型类的clone方法,通过实例化具体原型类建立“原型”
  • 访问类:使用具体原型类中的clone方法复制新的对象

原型模式的实现:

原型模式的实现分为“浅克隆”和“深克隆”,
浅克隆是指,创建一个新的对象,这个对象的基本数据类型属性与“原型”一样,非基本数据类型属性指向原型的对应属性对象。
深克隆是指,创建一个新的对象,这个对象的基本数据类型属性与“原型”一样,非基本数据类型属性也会被克隆,指向新地址。
浅克隆的代码实现:
在Java中,只需继承cloneable接口,并重写其中的clone方法,即可完成原型模式(浅克隆)。下述代码中,shallowClone1就是“原型”,其中studentnum属性是非基本数据类型属性。

/**
 * Cloneable是抽象原型类
 * ShallowClone是具体原型类
 */
public class ShallowClone implements Cloneable{
    private String name;
    private ArrayList<Integer> studentnum;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<Integer> getStudentnum() {
        return studentnum;
    }

    public void setStudentnum(ArrayList<Integer> studentnum) {
        this.studentnum = studentnum;
    }

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

/**
 * test是测试类,也是访问类
 * shallowClone1是"原型"
 */
public class test {
    public static void main(String[] args) throws CloneNotSupportedException {
        ShallowClone shallowClone1 = new ShallowClone();
        ArrayList<Integer> studentnum = new ArrayList<>();
        studentnum.add(1001);
        shallowClone1.setName("张三");
        shallowClone1.setStudentnum(studentnum);

        ShallowClone shallowClone2 = (ShallowClone) shallowClone1.clone();
        System.out.println(shallowClone1.getName().equals(shallowClone2.getName()));//true
        System.out.println(shallowClone1.getStudentnum() == shallowClone2.getStudentnum());//true
    }
}

深克隆的代码实现:
在java中实现深克隆,可以通过序列化与反序列化的方式实现,需要在具体原型类中实现Serializable接口。

public class ShallowClone implements Serializable {
    private String name;
    private ArrayList<Integer> studentnum;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<Integer> getStudentnum() {
        return studentnum;
    }

    public void setStudentnum(ArrayList<Integer> studentnum) {
        this.studentnum = studentnum;
    }
}

/**
 * test是测试类,也是访问类
 * shallowClone1是"原型"
 */
public class test {
    public static void main(String[] args) throws Exception {
        ShallowClone shallowClone1 = new ShallowClone();
        ArrayList<Integer> studentnum = new ArrayList<>();
        studentnum.add(1001);
        shallowClone1.setName("张三");
        shallowClone1.setStudentnum(studentnum);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/Users/wangrunfeng/a.txt"));
        oos.writeObject(shallowClone1);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/Users/wangrunfeng/a.txt"));
        ShallowClone shallowClone2 = (ShallowClone) ois.readObject();

        System.out.println(shallowClone1.getName().equals(shallowClone2.getName()));//true
        System.out.println(shallowClone1.getStudentnum() == shallowClone2.getStudentnum());//false
    }
}

文章内容为个人学习总结,如有错误望指正。
参考链接https://www.bilibili.com/video/BV1Np4y1z7BU

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值