原型-设计模式

原型设计模式

原型模式应用场景:创建一个对象比较复杂,当前存在一个和需要创建的对象极其相似,我们就可以采用原型模式,在原来的对象上进行一个修改。

修改方案:在原来的基础上进行拷贝,在进行部分的修改。(具体采用深拷贝和浅拷贝根据具体的业务场景进行选择)就像我们写一段文本时,前面已经写过一段极其相似的文本,我们可以直接拷贝,然后进行修改。提高了写文本的效率。

java 复制代码
package com.obstar.prototype;

public class MachineStatus {
    public String stable;

    public String getStable() {
        return stable;
    }

    public MachineStatus(String stable) {
        this.stable = stable;
    }

    public void setStable(String stable) {
        this.stable = stable;
    }

    @Override
    public String toString() {
        return "MachineStatus{" +
                "stable='" + stable + '\'' +
                '}';
    }
}
package com.obstar.prototype;

public class Machine implements Cloneable{
    private String model;
    private String speed;
    private String weight;
    private String power;
    private MachineStatus status;

    public Machine(String model, String speed, String weight, String power, MachineStatus status) {
        this.model = model;
        this.speed = speed;
        this.weight = weight;
        this.power = power;
        this.status = status;
    }

    public MachineStatus getStatus() {
        return status;
    }

    public void setStatus(MachineStatus status) {
        this.status = status;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getSpeed() {
        return speed;
    }

    public void setSpeed(String speed) {
        this.speed = speed;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }


    public String getPower() {
        return power;
    }

    public void setPower(String power) {
        this.power = power;
    }

    @Override
    public String toString() {
        return "Machine{" +
                "model='" + model + '\'' +
                ", speed='" + speed + '\'' +
                ", weight='" + weight + '\'' +
                ", power='" + power + '\'' +
                ", status='" + status + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Machine machine = (Machine)super.clone();
        MachineStatus machineStatus = new MachineStatus(this.status.stable);
        machine.setStatus(machineStatus);
        return machine;
    }
}

DEMO:

java 复制代码
public class Demo {
    public static void main(String[] args) throws CloneNotSupportedException {
        Machine machine1 = new Machine("A19", "3600", "1000KG"
            ,"5000W", new MachineStatus("稳定"));

        Machine machine2 = (Machine) machine1.clone();
        machine2.setModel("A20");

        System.out.println(machine1);
        System.out.println(machine2);
    }
}
相关推荐
学长毕业设计2 小时前
基于SpringBoot的公益基金管理系统(源码+文档+讲解视频)
java·spring boot·后端
东小西2 小时前
【SAA实战】第 3 篇 · 工具调用全攻略:把业务能力交给 Agent 自己调度
java·后端·spring
东小西2 小时前
【SAA实战】第 4 篇 · Agent 短期记忆:saver 让 Agent 跨轮记得住(threadId 隔离)
java·后端·spring
许彰午3 小时前
22-DataCenter报文序列化
java·低代码·架构·状态模式
2601_962065253 小时前
[MySQL] SQL优化之性能分析
java·sql·mysql
小范同学_3 小时前
JDK1.7 与 JDK1.8 HashMap 底层原理对比 + 数组并发扩容死循环详解
java·开发语言
予昊4 小时前
从零实现“在线五子棋对战“:WebSocket 实时通信 + 段位匹配
java·开发语言·网络·websocket
2601_962203514 小时前
【SpringAI入门】初识SpringAI
java
weixin_461408585 小时前
Mybatis-flex小记
java·开发语言·mybatis
2601_962177135 小时前
【MySQL篇】聚合查询,联合查询
android·java·mysql