原型-设计模式

原型设计模式

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

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

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);
    }
}
相关推荐
karry_k5 小时前
MyBatis批量insert-select踩坑:useGeneratedKeys=true 可能让PostgreSQL返回大量插入结果
java·后端
karry_k5 小时前
PostgreSQL 在 MyBatis 中执行正常 SQL 失效:一次 DELETE USING 踩坑记录
java·后端
SamDeepThinking9 小时前
从源码到代码:MyBatis-Flex 与 MyBatis-Plus 的逐项对比
java·后端·程序员
她的男孩12 小时前
Spring Boot 接 Flowable 工作流:用 3 个注解搭一个请假审批流程
java·后端·架构
荣码13 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
plainGeekDev15 小时前
Gson → kotlinx.serialization
android·java·kotlin
小bo波1 天前
Java Swing 图形用户界面实验 —— 从算术练习到游戏开发的完整实践
java·课程设计·gui·游戏开发·扫雷·swing
咖啡八杯1 天前
GoF设计模式——备忘录模式
java·后端·spring·设计模式