原型模式的用法

2023/6/7 1:23:03

本文主要是介绍原型模式的用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • 一、原型模式的用法
    • 1.1 介绍
    • 1.2 结构
    • 1.3 原型模式类图
    • 1.4 实现
      • 1.4.1 克隆的分类
      • 1.4.2 代码
    • 1.5 "三好学生"奖状案例
      • 1.5.1 "三好学生"奖状类图
      • 1.5.2 代码
    • 1.6 深、浅克隆的区分
      • 1.6.1 浅克隆
      • 1.6.2 深克隆

一、原型模式的用法

1.1 介绍

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

1.2 结构

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

1.3 原型模式类图

Java中的Object类中提供了 clone() 方法来实现浅克隆。 Cloneable 接口是类图中的抽象原型类,而实现了Cloneable接口的子实现类就是具体的原型类。

1.4 实现

1.4.1 克隆的分类

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

浅克隆:创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址。

深克隆:创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址。

1.4.2 代码

/**
 * 具体原型类: Realizetype
 */
public class Realizetype implements Cloneable{
    // 无参构造函数,创建对象时运行里面的代码
    public Realizetype() {
        System.out.println("具体的原型对象创建完成");
    }
    @Override
    public Realizetype clone() throws CloneNotSupportedException {
        System.out.println("具体原型复制成功");
        // Object类中提供了clone()方法来实现浅克隆,强转为Realizetype
        return (Realizetype) super.clone();
    }
}

/**
 * 访问类: client
 */
public class client{
    public static void main(String[] args) throws CloneNotSupportedException {
        // 创建一个原型类的对象
        Realizetype realizetype = new Realizetype();
        // 调用Realizetype类中的clone方法进行对象的克隆
        Realizetype clone = realizetype.clone();
        System.out.println("原型对象和克隆出来的对象是否为一个对象:" + (realizetype == clone));// false
    }
}

1.5 "三好学生"奖状案例

1.5.1 "三好学生"奖状类图

同一学校的“三好学生”奖状除了获奖人姓名不同,其他都相同,可以使用原型模式复制多个“三好学生”奖状出来,然后在修改奖状上的名字即可。

1.5.2 代码

/**
 * 学生实体类: Student
 */
public class Student {
    // 学生姓名
    private String name;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }
    
    public String toString() {
        return "Student{name = " + name + "}";
    }
}

/**
 * 具体原型类: Citation
 */
public class Citation implements Cloneable {
    private Student stu;

    public Student getStudent(){
        return stu;
    }

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

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

    public void show() {
        System.out.println(stu.getName() + "同学:在2023学年第一学期中表现优秀,被评为三好学生。特发此状!");
    }
}

/**
 * 访问类: CitaionTest
 */
public class CitaionTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        // 创建原型对象
        Citation citation = new Citation();
        // 创建张三学生对象
        Student stu = new Student();
        stu.setName("张三");
        citation.setStudent(stu);

        // 克隆奖状对象
        Citation citation1 = citation.clone();
        // 避免浅克隆的问题,重新声明一个对象
        Student stu1 = new Student();
        stu1.setName("李四");
        citation1.setStudent(stu1);

        // 调用show方法展示
        citation.show();
        citation1.show();
    }
}

1.6 深、浅克隆的区分

1.6.1 浅克隆

创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址。stu对象和stu1对象是同一个对象,就会产生将stu1对象中name属性值改为“李四”,两个Citation(奖状)对象中显示的都是李四。这就是浅克隆的效果,对具体原型类(Citation)中的引用类型的属性进行引用的复制

/**
 * 学生实体类: Student
 */
public class Student {
    // 学生姓名
    private String name;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }
    
    public String toString() {
        return "Student{name = " + name + "}";
    }
}

/**
 * 具体原型类: Citation
 */
public class Citation implements Cloneable {
    private Student stu;

    public Student getStudent(){
        return stu;
    }

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

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

    public void show() {
        System.out.println(stu.getName() + "同学:在2023学年第一学期中表现优秀,被评为三好学生。特发此状!");
    }
}

/**
 * 访问类: CitaionTest
 */
public class CitaionTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        // 创建原型对象
        Citation citation = new Citation();
        // 创建张三学生对象
        Student stu = new Student();
        stu.setName("张三");
        citation.setStudent(stu);
        // 克隆奖状对象
        Citation citation1 = citation.clone();
        Student stu1 = citation1.getStudent();
        stu1.setName("李四");

        //3,调用show方法展示
        citation.show();//李四同学:在2023学年第一学期中表现优秀,被评为三好学生。特发此状!
        citation1.show();//李四同学:在2023学年第一学期中表现优秀,被评为三好学生。特发此状!
    }
}

1.6.2 深克隆

创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址。深克隆需要使用对象流来实现。

注意:Citation类和Student类必须实现Serializable接口,否则会抛NotSerializableException异常。

/**
 * 学生实体类: Student
 */
public class Student implements Serializable {
    // 学生姓名
    private String name;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }
    
    public String toString() {
        return "Student{name = " + name + "}";
    }
}

/**
 * 具体原型类: Citation
 */
public class Citation implements Cloneable,Serializable {
    private Student stu;

    public Student getStudent(){
        return stu;
    }

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

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

    public void show() {
        System.out.println(stu.getName() + "同学:在2023学年第一学期中表现优秀,被评为三好学生。特发此状!");
    }
}

/**
 * 访问类: CitaionTest
 */
public class CitaionTest {
    public static void main(String[] args) throws Exception {
        // 创建原型对象
        Citation citation = new Citation();
        // 创建张三学生对象
        Student stu = new Student();
        stu.setName("张三");
        citation.setStu(stu);

        // 创建对象输出流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D://a.txt"));
        // 写对象
        oos.writeObject(citation);
        // 释放资源
        oos.close();

        // 创建对象输入流对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D://a.txt"));
        // 读取对象
        Citation citation1 = (Citation) ois.readObject();
        // 释放资源
        ois.close();
        Student stu1 = citation1.getStu();
        stu1.setName("李四");

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

记录每一个学习瞬间



这篇关于原型模式的用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程