原型-设计模式

原型设计模式

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

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

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);
    }
}
相关推荐
the beard几秒前
Maven 入门与进阶:聚合、继承与生命周期详解
java
楚禾Noah4 分钟前
【设计模式实战】原型模式 + 工厂模式:AI Agent 配置中心
人工智能·设计模式·原型模式
csdn_aspnet9 分钟前
解决 Spring Boot 应用程序中的“无法配置数据源”错误
java·spring boot
Pure_Eyes19 分钟前
设计模式详解
设计模式
hai_qin21 分钟前
三,设计模式-抽象工厂模式
c++·设计模式·抽象工厂模式
灵魂猎手39 分钟前
9. Mybatis与Spring集成原理解析
java·后端·源码
AAA修煤气灶刘哥42 分钟前
避坑!线程 / 线程池从入门到华为云实战,面试官听了都点头
java·后端·面试
Agome991 小时前
Docker之nginx安装
java·nginx·docker
java1234_小锋2 小时前
说说你对Integer缓存的理解?
java·开发语言
至此流年莫相忘2 小时前
TypeReference 泛型的使用场景及具体使用流程
java·开发语言·spring boot