• 原型设计模式


    1、实现 

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

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

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

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

    1. package com.jmj.pattern.prototype.demo;
    2. public class Realizetype implements Cloneable{
    3. public Realizetype() {
    4. System.out.println("具体的原型对象创建完成!");
    5. }
    6. @Override
    7. protected Realizetype clone() throws CloneNotSupportedException {
    8. System.out.println("具体原型复制成功!");
    9. return (Realizetype)super.clone();
    10. }
    11. }
    1. package com.jmj.pattern.prototype.demo;
    2. public class Client {
    3. public static void main(String[] args) throws CloneNotSupportedException {
    4. Realizetype realizetype = new Realizetype();
    5. System.out.println(realizetype);
    6. //没有再次执行无参构造
    7. Realizetype clone = realizetype.clone();
    8. System.out.println(clone);
    9. }
    10. }

    1. package com.jmj.pattern.prototype.test;
    2. import java.io.FilterOutputStream;
    3. public class Citation implements Cloneable{
    4. //三好学生上的姓名
    5. private String name;
    6. public String getName() {
    7. return name;
    8. }
    9. public void setName(String name) {
    10. this.name = name;
    11. }
    12. public void show(){
    13. System.out.println(name+"同学:在2020学年第一学期中表现优秀,被评为三好学生。特发此状!");
    14. }
    15. @Override
    16. public Citation clone() throws CloneNotSupportedException {
    17. return (Citation) super.clone();
    18. }
    19. }
    1. package com.jmj.pattern.prototype.test;
    2. public class CitationTest {
    3. public static void main(String[] args) throws CloneNotSupportedException {
    4. //1.创建原型对象
    5. Citation citation = new Citation();
    6. Citation clone = citation.clone();
    7. clone.setName("张三");
    8. citation.setName("李四");
    9. citation.show();
    10. clone.show();
    11. }
    12. }

    1. package com.jmj.pattern.prototype.test11;
    2. import java.io.*;
    3. public class CitationTest {
    4. public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
    5. //1.创建原型对象
    6. Citation citation = new Citation();
    7. Student student = new Student();
    8. student.setName("张三");
    9. citation.setStudent(student);
    10. ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("src/main/resources/static/b.txt"));
    11. oos.writeObject(citation);
    12. oos.close();
    13. ObjectInputStream ois=new ObjectInputStream(new FileInputStream("src/main/resources/static/b.txt"));
    14. Citation o = (Citation) ois.readObject();
    15. ois.close();
    16. o.getStudent().setName("李四");
    17. citation.show();
    18. o.show();
    19. }
    20. }

     总结:浅克隆 实现Cloneable接口 重写clone方法 。       

              深克隆  用序列化和反序列化实现深克隆。

    
                    
  • 相关阅读:
    小样本学习(Few-shot Learning)
    API自动化(四)
    FX5800计算器测量程序集2.4
    前台自动化测试:基于敏捷测试驱动开发(TDD)的自动化测试原理
    【MySQL】MySQL分区表详解
    注册【小程序】和注册页面
    SSM+Vue+Element-UI实现网上跳蚤市场
    确保数据可视化的准确性:后校验的重要性和方法
    Linux 通过 Docker 部署 Nacos 2.2.3 服务发现与配置中心
    打造人脸磨皮算法新标杆,满足企业多元化需求
  • 原文地址:https://blog.csdn.net/qq_53374893/article/details/134376654