【设计模式 05】原型模式

有的时候,我们创建对象,需要耗费大量时间在一些资源型操作上,这个时候,我们就可以先创建出一个模板,然后每次创建的时候直接从模板复制即可,不用反复进行耗时的资源型操作。

python代码:

python 复制代码
import copy

class ComplexObject:
    def __init__(self, data):
        # 资源型操作 
        self.data = data
    
    def clone(self):
        # 复制
        return copy.deepcopy(self)

# 创建原型对象
obj1 = ComplexObject(data = "large data")
# 创建新对象,直接拷贝原对象
new_object = original_object.clone()

JAVA代码:

java 复制代码
// 1. 定义抽象原型类
public abstract class Prototype  implements Coneable {
    public abstract Prototype clone();
}
// 2. 定义具体原型类
public class ConcretePrototype extends Prototype {
    private String data;

    public ConcretePrototype(String data) {
        this.data = data;
    }

    @Override
    public Prototype clone() {
        return new ConcretePrototype(this.data);
    }

    public String getData() {
        return data;
    }
}

// 3. 客户端代码
public class Client {
    public static void main(String[] args) {
        // 创建原型对象
        Prototype original = new ConcretePrototype("原始对象");

        // 克隆原型对象
        Prototype clone = original.clone();

        // 输出克隆对象的数据
        System.out.println("Clone Data: " + ((ConcretePrototype) clone).getData());
    }
}

【设计模式专题之原型模式】5. 矩形原型

cpp 复制代码
#include<iostream>
#include<string>
#include<vector>
using namespace std;

// 抽象原型类
class Prototype {
public:
    virtual Prototype* clone() const = 0;
    virtual string getDetails() const = 0;
    virtual ~Prototype() {}
};

// 具体矩形原型类
class RectanglePrototype : public Prototype {
private:
    string color;
    int width;
    int height;
public:
    // 构造方法
    RectanglePrototype(const string& color, int width, int height) : color(color), width(width), height(height) {}

    // 克隆方法
    Prototype* clone() const override {
        return new RectanglePrototype(*this);
    }

    string getDetails() const override {
        return "Color: " + color + ", Width: " + to_string(width) + ", Height: " + to_string(height);
    }
};

// 客户端 
int main() {
    vector<Prototype*> rectangles;

    // 读取需要创建的矩形数量
    int N;
    cin >> N;

    // 地区每个矩形的属性星系并创建矩形对象
    for (int i = 0; i < N; ++i) {
        string color;
        int width, height;

        cin >> color >> width >> height;

        // 创建原型对象
        Prototype* originalRectangle = new RectanglePrototype(color, width, height);

        // 将原型对象保存到向量中
        rectangles.push_back(originalRectangle);
    }

    // 克隆对象
    for (const auto& rectangle : rectangles) {
        Prototype* cloneRectangle = rectangle->clone();
        cout << cloneRectangle->getDetails() << endl;

        // 释放克隆对象的内存
        delete cloneRectangle;
    }

    // 释放原型对象的内存
    for (const auto& rectangle : rectangles) {
        delete rectangle;
    }

    return 0;
}
相关推荐
码哥DFS12 小时前
构造函数、实例对象、对象原型 ----三者关系
开发语言·javascript·原型模式
AI人工智能+电脑小能手17 小时前
大白话说Java设计模式-08-建造者模式(业务实战篇)
java·设计模式·建造者模式·架构设计·对象构建
AustinXu20 小时前
从 Claude Code 到 Claude Tag,Harness Engineering 走到了组织这一层
设计模式·团队管理
剧中有戏20 小时前
单例模式从入门到精通:一个数据库连接池的完整剖析
设计模式
胡萝卜术20 小时前
编译期与运行期的双重防线:从 TypeScript 类型之争到 LLM 输出的自动化择优
前端·设计模式·面试
货拉拉技术20 小时前
重塑 Agent 度量衡:基于 LLM-as-a-Judge 的离线评估体系与实践
算法·设计模式
KhalilRuan21 小时前
设计模式小记
设计模式
卡牌RWA研究院21 小时前
Relique:一张精品卡牌如何从收藏品变成可交易的链上资产?
设计模式·金融·区块链·创业创新
剧中有戏2 天前
抽象工厂模式从入门到精通:一个多云存储案例的完整剖析(修订版)
设计模式
MC皮蛋侠客2 天前
Redis 系列(八):缓存设计模式与一致性——从 Cache Aside 到防雪崩
redis·缓存·设计模式