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

原型模式的基本实现

创建⼀个抽象类或接⼝,声明⼀个克隆⽅法 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


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 类,用于创建文档对象。你可以使用原型模式来复制文档——示例:

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);
    }
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值