软件设计之原型模式

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

注意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());
    }
}

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

相关推荐
冰糖雪梨dd21 小时前
JS中new的过程发生了什么
开发语言·javascript·原型模式
white-persist4 天前
SQL 注入详解:从原理到实战
前端·网络·数据库·sql·安全·web安全·原型模式
white-persist4 天前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
魔云连洲7 天前
深入解析:Object.prototype.toString.call() 的工作原理与实战应用
前端·javascript·原型模式
white-persist9 天前
Burp Suite模拟器抓包全攻略
前端·网络·安全·web安全·notepad++·原型模式
青草地溪水旁9 天前
第五章:原型模式 - 克隆大法的大师
c++·设计模式·原型模式
white-persist9 天前
【burp手机真机抓包】Burp Suite 在真机(Android and IOS)抓包手机APP + 微信小程序详细教程
android·前端·ios·智能手机·微信小程序·小程序·原型模式
XiaoLeisj13 天前
【SpringAI】第四弹:深入解析 Rag 检索增强工作流程、最佳实践和调优
阿里云·原型模式·rag·spring ai·灵积大模型
CoderIsArt14 天前
四种对象型创建模式:抽象工厂、 build模式、原型ProtoType与单例模式
单例模式·原型模式
Misnearch15 天前
原型模式了解
原型模式