Gof23设计模式之原型模式

1.概述

用一个已经创建的实例作为原型,通过复制该原型对象来创建一个和原型对象相同的新对象。

2.结构

原型模式包含一下角色:

  • 抽象原型类:规定了具体原型对象必须实现的clone()方法
  • 具体原型类:实现了抽象圆形类的clone()方法,它是可被复制的对象
  • 访问类:使用具体原型类中的clone()方法来复制新的对象

3.类图

在这里插入图片描述

4.实现

原型模式的克隆分为浅克隆和深克隆。

  • 浅拷贝:创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址。
  • 深拷贝:创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址。

4.1.浅拷贝

/**
 * @author 晓风残月Lx
 * @date 2023/6/25 22:51
 */
public class Citation implements Cloneable {

    // 姓名
    private Student student;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public Citation clone() throws CloneNotSupportedException {
        return (Citation) super.clone();
    }

    public void show() {
        System.out.println(student.getName() + "同学,表现挺好,给你个大拇哥,相信我做你自己绝壁泰酷啦");
    }
}
/**
 * @author 晓风残月Lx
 * @date 2023/6/25 22:57
 */
public class Student {

    // 学生姓名
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
/**
 * @author 晓风残月Lx
 * @date 2023/6/25 22:59
 *      浅拷贝的非基本类型属性,扔指向原有属性所指向同一个内存地址
 */
public class CitationTest {
    public static void main(String[] args) throws CloneNotSupportedException {

        Citation citation = new Citation();
        Student student = new Student();
        student.setName("晓风残月Lx");
        citation.setStudent(student);

        Citation clone = citation.clone();
        clone.getStudent().setName("小吕");

        citation.show();
        clone.show();
    }
}

在这里插入图片描述
这个运行结果说明,在CitationTest中克隆后的Citation类中的Student类和原型Citation类中的Student类是同一个内存地址

4.2.深拷贝

深拷贝需要对象流,并且需要实现序列化接口

import java.io.Serializable;

/**
 * @author 晓风残月Lx
 * @date 2023/6/25 22:51
 */
public class Citation implements Cloneable, Serializable {

    // 姓名
    private Student student;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public Citation clone() throws CloneNotSupportedException {
        return (Citation) super.clone();
    }

    public void show() {
        System.out.println(student.getName() + "同学,表现挺好,给你个大拇哥,相信我做你自己绝壁泰酷啦");
    }

}
import java.io.Serializable;

/**
 * @author 晓风残月Lx
 * @date 2023/6/25 22:57
 */
public class Student implements Serializable {

    // 学生姓名
    private String name;

    public String getName() {
        return name;
    }

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

}

import java.io.*;

/**
 * @author 晓风残月Lx
 * @date 2023/6/25 22:59
 *      深拷贝需要对象流   里面的对象也就是student不是同一个
 */
public class CitationTest {
    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {

        Citation citation = new Citation();
        Student student = new Student();
        student.setName("晓风残月Lx");
        citation.setStudent(student);

        // 创建对象输出流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\software\\Java\\IDEA\\workspace\\design_gof23\\b.txt"));
        oos.writeObject(citation);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\software\\Java\\IDEA\\workspace\\design_gof23\\b.txt"));
        Citation citation1 = (Citation) ois.readObject();
        citation1.getStudent().setName("小吕");
        ois.close();

        citation.show();
        citation1.show();

    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓风残月Lx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值