Java设计模式---原型模式

原型模式

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

  • 结构:

    • 抽象原型类:规定了具体原型对象必须实现的clone()方法
    • 具体原型类:实现抽象原型类的clone()方法,它是可被复制的对象
    • 访问类:使用具体原型类中的clone()方法来复制新的对象
  • 实现:
    原型模式的克隆分为浅克隆和深克隆

    • 浅克隆:创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址
    • 深克隆:创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址
      Java中的Object类提供了clone()方法来实现浅克隆。Cloneable接口是抽象原型类,而实现了Cloneable接口的子实现类就是具体的原型类
  • 浅克隆

package com.itheima.pattern.prototype.demo;

// 原型模式
public class Realizetype implements Cloneable{
    public Realizetype() {
        System.out.println("具体原型对象创建完成!");
    }

    @Override
    protected Realizetype clone() throws CloneNotSupportedException {
        System.out.println("具体原型复制成功!");
        return (Realizetype) super.clone();
    }
}

测试类

package com.itheima.pattern.prototype.demo;

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));
    }
}

运行结果
浅克隆

  • 三好学生案例
package com.itheima.pattern.prototype.test;

public class Citation implements Cloneable{
    // 三好学生的姓名
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    protected Citation clone() throws CloneNotSupportedException {
        return (Citation) super.clone();
    }
    public void show(){
        System.out.println(name+"同学被评为三好学生...");
    }
}

测试代码

package com.itheima.pattern.prototype.test;

public class CitationTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        // 创建原型对象
        Citation citation = new Citation();
        // 克隆奖状对象
        Citation citation1 = citation.clone();
        citation.setName("张三");
        citation1.setName("李四");

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

运行结果
三好学生

  • 使用场景
    • 对象的创建非常复杂,可以使用原型模式快捷的创建对象
    • 性能和安全要求比较高

定义Student

package com.itheima.pattern.prototype.dptest;

public class Student {
    // 学生姓名
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}

package com.itheima.pattern.prototype.dptest;

public class Citation implements Cloneable{

    private Student student;

    public Student getStudent() {
        return student;
    }

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

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

    public void show(){
        System.out.println(student.getName()+"被评为三号学生。。。");
    }
}

测试代码

package com.itheima.pattern.prototype.dptest;

public class CitationTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        // 创建原型对象
        Citation citation = new Citation();
        // 创建张三学生对象
        Student student = new Student();
        student.setName("张三");
        citation.setStudent(student);

        // 克隆奖状对象
        Citation citation1 = citation.clone();
        Student student1 = citation1.getStudent();
        student1.setName("李四");
        // 调用show方法展示
        citation.show();
        citation1.show();
    }
}

运行结果
浅克隆

student和student1对象是同一个对象,就会产生将student1对象中name属性改为”李四“,两个Citation对象中显示的都是李四。
这就是浅克隆的效果,对具体原型类(Citation)中的引用类型的属性进行引用的复制。这种情况需要使用深克隆,而进行深克隆需要
使用对象流

【解决方法】深克隆

package com.itheima.pattern.prototype.dptest1;

import java.io.Serializable;

public class Student implements Serializable {
    // 学生姓名
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}

package com.itheima.pattern.prototype.dptest1;

import java.io.Serializable;

public class Citation implements Cloneable, Serializable {

    private Student student;

    public Student getStudent() {
        return student;
    }

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

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

    public void show(){
        System.out.println(student.getName()+"被评为三号学生。。。");
    }
}

测试代码

package com.itheima.pattern.prototype.dptest1;

import java.io.*;

public class CitationTest {
    public static void main(String[] args) throws Exception {
        // 创建原型对象
        Citation citation = new Citation();
        // 创建张三学生对象
        Student student = new Student();
        student.setName("张三");
        citation.setStudent(student);

        // 创建对象输出流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\Java\\source_code\\DataStructures\\src\\com\\itheima\\pattern\\prototype\\dptest1\\dpTest1.txt"));
        // 写对象
        oos.writeObject(citation);
        // 释放资源
        oos.close();
        // 创建对象输入流对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\Java\\source_code\\DataStructures\\src\\com\\itheima\\pattern\\prototype\\dptest1\\dpTest1.txt"));
        // 读对象
        Citation citation1 = (Citation) ois.readObject();
        // 释放资源
        ois.close();
        Student student1 = citation1.getStudent();
        student1.setName("李四");
        citation.show();
        citation1.show();

    }
}

运行结果
深拷贝

注意:Citation类和Student类必须实现Serializable接口,否则会抛NotSerializableException异常
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

SimpleZihao

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

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

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

打赏作者

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

抵扣说明:

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

余额充值