【设计模式之原型模式——矩形原型】

原型模式的基本实现

创建⼀个抽象类或接⼝,声明⼀个克隆⽅法 clone

具体原型类去实现接口,重写克隆⽅法

客户端中实例化具体原型类的对象,并调⽤其克隆⽅法来(赋给)创建新的对象。


什么时候实现原型模式 ?

相⽐于直接实例化对象,通过原型模式复制对象可以减少资源消耗,提⾼性能,尤其在对象的创建过程复杂或对象 的创建代价较⼤的情况下。当需要频繁创建相似对象、并且可以通过克隆避免重复初始化⼯作的场景时可以考虑使 ⽤原型模式,在克隆对象的时候还可以动态地添加或删除原型对象的属性,创造出相似但不完全相同的对象,提⾼了灵活性。 但是使⽤原型模式也需要考虑到如果对象的内部状态包含了引⽤类型的成员变量,那么实现深拷⻉就会变得较为复杂,需要考虑引⽤类型对象的克隆问题。

题目描述

公司正在开发一个图形设计软件,其中有一个常用的图形元素是矩形。设计师在工作时可能需要频繁地创建相似的矩形,而这些矩形的基本属性是相同的(颜色、宽度、高度),为了提高设计师的工作效率,请你使用原型模式设计一个矩形对象的原型。使用该原型可以快速克隆生成新的矩形对象。


输入描述

首先输入一个字符串,表示矩形的基本属性信息,包括颜色、长度和宽度,用空格分隔,例如 "Red 10 5"。

然后输入一个整数 N(1 ≤ N ≤ 100),表示使用原型创建的矩形数量。

输出描述

对于每个矩形,输出一行字符串表示矩形的详细信息,如 "Color: Red, Width: 10,Height: 5"。

输入示例

Red 10 5

3

输出示例

Color: Red, Width: 10, Height: 5

Color: Red, Width: 10, Height: 5

Color: Red, Width: 10, Height: 5


java 复制代码
import java.util.Scanner;


class OrginalObject implements Cloneable{
    private String color;
    private int width;
    private int height;
    //初始化
    public OrginalObject(String color,int width,int height){
        this.color=color;
        this.width=width;
        this.height=height;
    }
    // 克隆方法
     @Override
    public OrginalObject clone(){
      try{
        return (OrginalObject) super.clone();//重写 clone()方法时,你需要将Object类型转换为你的具体类类型
      }catch(CloneNotSupportedException e){
          e.printStackTrace();
          return null;
      }
    }
    // 获取详细信息
    public String getResult(){
        return "Color:" + color + ",Width:" + width + ",Height:" + height;
    }
}

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String color=sc.next();
        int width=sc.nextInt();
        int height=sc.nextInt();
        //创建原型对象
        OrginalObject orginalOb=new OrginalObject(color,width,height);
        //所需创建矩形的数量
        int n=sc.nextInt();
        for(int i=0; i<n; i++){
            //原型调用clone方法给予clonedObject,原型类可以提供对象的复制功能
            OrginalObject clonedObject=orginalOb.clone();
            System.out.println(clonedObject.getResult());
        }
    }
}

假设你有一个 Document 类,用于创建文档对象。你可以使用原型模式来复制文档------示例:

java 复制代码
import java.util.HashMap;
import java.util.Map;

// 原型接口
public interface Prototype {
    Prototype clone();
}

// 具体原型类
public class Document implements Prototype {
    private Map<String, Object> attributes;

    public Document() {
        attributes = new HashMap<>();
    }

    public void setAttribute(String key, Object value) {
        attributes.put(key, value);
    }

    public Object getAttribute(String key) {
        return attributes.get(key);
    }

    @Override
    public Document clone() {
        Document clone = new Document();
        clone.attributes.putAll(this.attributes); // 复制所有属性
        return clone;
    }

    @Override
    public String toString() {
        return "Document{attributes=" + attributes + "}";
    }
}

// 客户端代码
public class Main {
    public static void main(String[] args) {
        // 创建一个原始文档对象
        Document originalDocument = new Document();
        originalDocument.setAttribute("Title", "Original Title"); // 设置 Title 属性
        originalDocument.setAttribute("Content", "Original Content"); // 设置 Content 属性

        // 通过原型模式复制文档
        Document clonedDocument = originalDocument.clone();//克隆当前对象,并复制所有属性到新对象中
        clonedDocument.setAttribute("Title", "Cloned Title");// 修改克隆文档的 Title 属性
        clonedDocument.setAttribute("Author", "New Author");// 添加新的属性 Author


        // 输出结果
        System.out.println("Original Document: " + originalDocument);
        System.out.println("Cloned Document: " + clonedDocument);
    }
}
相关推荐
-凌凌漆-1 小时前
【设计模式】装饰模式
设计模式
蔚一1 小时前
Java设计模式—面向对象设计原则(一) ----->开闭原则OCP(完整详解,附有代码+案例)
java·后端·设计模式·intellij-idea·开闭原则
丶白泽4 小时前
重修设计模式-结构型-代理模式
设计模式·系统安全·代理模式
Java__攻城狮6 小时前
java设计模式(持续更新中)
java·开发语言·设计模式
王佑辉6 小时前
【软考】设计模式之抽象工厂模式
设计模式·软考
会敲代码的小张6 小时前
设计模式-外观模式
java·开发语言·后端·设计模式·外观模式
AI让世界更懂你7 小时前
漫谈设计模式 [9]:外观模式
python·设计模式·外观模式
LB_bei10 小时前
设计模式-行为型模式-备忘录模式
设计模式·备忘录模式
心之语歌11 小时前
设计模式 桥接模式(Bridge Pattern)
java·设计模式·桥接模式
LB_bei13 小时前
设计模式-行为型模式-访问者模式
设计模式·访问者模式