原型模式(prototype)

原型是一种创建型设计模式, 使你能够复制对象, 甚至是复杂对象, 而又无需使代码依赖它们所属的类。所有的原型类都必须有一个通用的接口, 使得即使在对象所属的具体类未知的情况下也能复制对象。

原型模式分为浅复制和深复制。
浅复制:将一个对象复制之后,生成一个新的对象,新对象的所有成员变量(基本类型或引用类型)都含有与原有对象相同的值,如果原有对象的成员变量是基本数据类型,就会将这个变量的值复制一份到新对象里面,如果原有对象的成员变量是引用数据类型,那么这个引用指向的对象不会新生成一份,而是,在新对象里面的这个引用与原有对象的引用指向的是同一个对象。

浅复制,基本数据类型

1. Citation.java

java 复制代码
package prototype2;

/**
 * Author: 
 * Created: 2024/09/29 14:01
 * Description:
 */
public class Citation implements Cloneable {

    private String name;

    public String getName() {
        return name;
    }

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

    public void show() {
        System.out.println(name + "同学: 表现优秀,特发此奖状!!!");
    }

    @Override
    protected Citation clone() throws CloneNotSupportedException {
        return (Citation) super.clone();
    }
}
复制代码
CitationTest.java
java 复制代码
package prototype2;

/**
 * Author: 
 * Created: 2024/09/29 14:05
 * Description:
 */
public class CitationTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        //1.创建原型对象
        Citation citation = new Citation();
        //克隆奖状对象
        Citation citation2 = citation.clone();

        citation.setName("张三");
        citation2.setName("李四");

        citation.show();
        citation2.show();

        System.out.println(citation == citation2);
    }
}

执行结果:

张三同学: 表现优秀,特发此奖状!!!

李四同学: 表现优秀,特发此奖状!!!

false

Process finished with exit code 0

2. 浅复制,引用数据类型

Citation.java

java 复制代码
package prototype3;

/**
 * Author: 
 * Created: 2024/09/29 14:01
 * Description:
 */
public class Citation implements Cloneable {

    private Student student;

    public Student getStudent() {
        return student;
    }

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

    public void show() {
        System.out.println(student.getName() + "同学: 表现优秀,特发此奖状!!!");
    }

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

Student.java

java 复制代码
package prototype3;

/**
 * Author: 
 * Created: 2024/09/29 14:01
 * Description:
 */
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 + '\'' +
                '}';
    }
}
复制代码
CitationTest.java
java 复制代码
package prototype3;

/**
 * Author: 
 * Created: 2024/09/29 14:05
 * Description:
 */
public class CitationTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        //1.创建原型对象
        Citation citation = new Citation();
        Student student = new Student();
        student.setName("张三");
        citation.setStudent(student);


        //克隆奖状对象
        Citation citation2 = citation.clone();
        citation2.getStudent().setName("李四");

        citation.show();
        citation2.show();

        System.out.println(citation == citation2);
        Citation citation3 = citation.clone();
    }
}

执行结果:

李四同学: 表现优秀,特发此奖状!!!
李四同学: 表现优秀,特发此奖状!!!
false

Process finished with exit code 0

浅复制在引用数据类型时,引用指向的对象不会新生成一份,当修改student对象name时,原来被复制的对象的引用类型student的name属性也被改成了"李四",为了实现深复制,需要采用流的形式读入当前对象的二进制输入,再写出该二进制数据对应的对象。

深复制:将一个对象复制之后,生成一个新的对象,新对象的基本数据类型变量含有与原有对象相同的值,如果原有对象的成员变量是引用数据类型,在新对象里面,这些引用变量将指向被复制过的新对象,而不再是指向原有的那些被引用的对象,深复制把要复制的对象所引用的对象都复制一遍。为了实现深复制,需要采用流的形式读入当前对象的二进制输入,再写出该二进制数据对应的对象。

3. 深复制,引用数据类型

复制代码
Citation.java
java 复制代码
package prototype4;

import java.io.Serializable;

/**
 * Author: 
 * Created: 2024/09/29 14:01
 * Description:
 */
public class Citation implements Cloneable, Serializable {

    private Student student;

    public Student getStudent() {
        return student;
    }

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

    public void show() {
        System.out.println(student.getName() + "同学: 表现优秀,特发此奖状!!!");
    }

    @Override
    protected Citation clone() throws CloneNotSupportedException {
        return (Citation) super.clone();
    }
}
复制代码
Student.java
java 复制代码
package prototype4;

import java.io.Serializable;

/**
 * Author: 
 * Created: 2024/09/29 14:01
 * Description:
 */
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 + '\'' +
                '}';
    }
}
复制代码
CitationTest.java
java 复制代码
package prototype4;

import java.io.*;

/**
 * Author: 
 * Created: 2024/09/29 14:05
 * Description:
 */
public class CitationTest {
    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
        //1.创建原型对象
        Citation citation = new Citation();
        Student student = new Student();
        student.setName("张三");
        citation.setStudent(student);

        //创建对象输出流对象
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:/IdeaProjects/design-pattern/protype"));
        //写对象
        objectOutputStream.writeObject(citation);
        //释放资源
        objectOutputStream.close();

        //创建对象输入流对象
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("d:/IdeaProjects/design-pattern/protype"));
        //读取对象
        Citation citation2 = (Citation) objectInputStream.readObject();
        objectInputStream.close();

        citation2.getStudent().setName("李四");

        citation.show();
        citation2.show();

    }
}

执行结果:

张三同学: 表现优秀,特发此奖状!!!
李四同学: 表现优秀,特发此奖状!!!

Process finished with exit code 0

相关推荐
2603_965896621 天前
JS原型与原型链|对象继承与实例底层原理
开发语言·javascript·原型模式
YYYing.3 天前
【设计模式系列 (五) 】原型模式
开发语言·后端·设计模式·原型模式·c/c++
羞儿22 天前
【读点论文】Overall positive prototype for few-shot open-set recognition
原型模式·小样本学习·特征优化·开集学习
liangshanbo121523 天前
手写 Function.prototype.call 与 Function.prototype.apply 面试题满分答案
开发语言·javascript·原型模式
江畔柳前堤23 天前
云原生 × AI 全景图谱:从 Kubernetes 到 Agent 基础设施的一次认知跃迁
人工智能·分布式·gpt·云原生·kubernetes·原型模式·agi
艾伦野鸽ggg24 天前
JavaScript 原型链与原型对象详解
javascript·原型模式
AgentMaster24 天前
元数据、血缘、质量、安全四大模块能力拆解,数据治理方案对比:4 种技术路线深度评测
大数据·数据库·数据仓库·人工智能·原型模式
江畔柳前堤1 个月前
具身智能全景深度指南(2026年9月版):从“会聊天的AI“到“能干活的机器“
大数据·javascript·图像处理·人工智能·分布式·智慧城市·原型模式
神奇霸王龙1 个月前
Codex MCP GA 实测:Qwen / GLM / Kimi / DeepSeek / MiniMax 调度 Codex 沙箱的真实成本
人工智能·ai·agent·ai编程·原型模式·mcp
葡萄城技术团队1 个月前
如何通过 JSON 数据源 / Web API 快速构建轻量级报表(Report)?
前端·json·原型模式