软件设计之原型模式

原型模式是从一个对象再创建另一个可定制的对象,而且不需要知道任何创建的细节。拷贝分浅拷贝和深拷贝。浅拷贝无法拷贝引用对象。在面试的时候,我们会投多家公司,根据岗位的不同我们会适当调整。使用原型模式可以快速达到需求,下面通过这个案例说明。

注意WorkExperience要实现Serializable接口。

复制代码
package Prototype;

import java.io.Serializable;

public class WorkExperience implements Serializable {
    public String workDate;//日期
    public String position;//岗位
}

package Prototype;

import java.io.*;

public class Resume implements Cloneable, Serializable {
    private String name;//姓名
    private int age;//年龄
    private WorkExperience we = new WorkExperience();//工作经验

    public Resume(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setWorkExperience(String workDate, String position) {
        we.workDate = workDate;
        we.position = position;
    }
    public Resume clone() {
        Resume resume = null;
        try {
            resume = (Resume)super.clone();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resume;
    }

    public Resume deepClone() {
        Resume resume = null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            //序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            //反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            resume = (Resume) ois.readObject();
            return resume;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                bos.close();
                oos.close();
                bis.close();
                ois.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String toString() {
        return "Resume{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", we.workDate=" + we.workDate +
                ",we.position=" + we.position +
                '}';
    }
}

package Prototype;

public class Client {
    public static void main(String[] args) {
        Resume a = new Resume("张三",20);
        a.setWorkExperience("2023-1","Java工程师");
        Resume b = a.clone();
        b.setWorkExperience("2015-10","前端工程师");
        Resume c = a.deepClone();
        c.setWorkExperience("2015-5","网络工程师");
        System.out.println(a.toString());
        System.out.println(b.toString());
        System.out.println(c.toString());
    }
}

第一张简历是原型,第二张是第一张的浅拷贝并且修改了一定的信息,发现第一张与第二张有关工作经验的内容是一样的,因为这两张简历的工作经验指向的是同一个对象,而第三张是深拷贝,引用对象的信息被修改。

相关推荐
方见华Richard22 分钟前
整数阶时间重参数化:基于自适应豪斯多夫维数的偏微分方程正则化新框架
人工智能·笔记·交互·原型模式·空间计算
方见华Richard2 天前
世毫九《认知几何学修订版:从离散概念网络到认知拓扑动力学》
人工智能·经验分享·交互·原型模式·空间计算
方见华Richard2 天前
自指系统的安全本体论:论内生安全性的哲学基础与形式化路径
人工智能·经验分享·交互·学习方法·原型模式
xianyinsuifeng2 天前
RAG + Code Analysis 的标准路线
数据仓库·自动化·云计算·原型模式·aws
Beginner x_u4 天前
JavaScript 原型、原型链与原型继承的核心机制解析
开发语言·javascript·原型模式·原型原型链
方见华Richard5 天前
递归对抗引擎(RAE)核心极简实现框架
人工智能·交互·学习方法·原型模式·空间计算
方见华Richard5 天前
递归对抗引擎RAE V2.0(多智能体分布式对抗版)
人工智能·经验分享·交互·学习方法·原型模式
方见华Richard5 天前
递归对抗引擎RAE V3.0(碳硅共生版)
人工智能·经验分享·学习方法·原型模式·空间计算
懵萌长颈鹿6 天前
原型模式 (Prototype Pattern)
原型模式
2601_949480067 天前
Flutter for OpenHarmony音乐播放器App实战:定时关闭实现
javascript·flutter·原型模式