原型模式需要深拷贝是因为浅拷贝会导致新旧对象共享内部资源,修改一个对象的数据可能影响另一个对象,破坏对象独立性。1. 深拷贝确保每个对象拥有独立的资源副本,避免数据干扰;2. 使用智能指针如 std::unique_ptr 可自动管理内存,防止内存泄漏;3. 对于多态成员,需递归调用 clone() 方法,确保整个对象图正确复制。这三方面共同保障了原型模式中克隆对象的完整性和独立性。

在C++里实现原型模式(Prototype Pattern),说白了就是通过复制现有对象来创建新对象,而不需要知道具体是哪个类。这里头最核心、也最容易让人头疼的,就是怎么确保“深拷贝”,以及你的克隆接口该怎么设计才能既安全又好用。它不是简单的内存复制,而是要确保复制出来的新对象和原对象之间,即便内部有指针或者复杂结构,也能保持完全的独立性。

原型模式的核心在于一个抽象的基类,它声明了一个用于克隆自身的纯虚函数。然后,每个具体的派生类负责实现这个克隆方法,通过调用自身的拷贝构造函数来创建一个全新的、独立的副本。这里,我们倾向于使用智能指针,比如
std::unique_ptr

#include <iostream>
#include <string>
#include <memory> // For std::unique_ptr
// 抽象原型基类
class Shape {
public:
virtual ~Shape() = default; // 虚析构函数,确保正确释放内存
// 纯虚克隆函数,返回一个指向新创建对象的智能指针
virtual std::unique_ptr<Shape> clone() const = 0;
virtual void draw() const = 0;
};
// 具体原型类:圆形
class Circle : public Shape {
private:
int radius;
// 假设圆形内部有一个复杂的动态分配数据,需要深拷贝
std::unique_ptr<std::string> color;
public:
Circle(int r = 0, const std::string& c = "red") : radius(r), color(std::make_unique<std::string>(c)) {
// std::cout << "Circle constructor: " << r << ", " << *color << std::endl;
}
// 拷贝构造函数:执行深拷贝
Circle(const Circle& other) : radius(other.radius) {
if (other.color) {
color = std::make_unique<std::string>(*other.color); // 深拷贝color指向的数据
} else {
color = nullptr;
}
// std::cout << "Circle deep copy constructor called." << std::endl;
}
// 拷贝赋值运算符(虽然原型模式主要依赖拷贝构造,但为了完整性)
Circle& operator=(const Circle& other) {
if (this != &other) {
radius = other.radius;
if (other.color) {
color = std::make_unique<std::string>(*other.color);
} else {
color = nullptr;
}
}
return *this;
}
// 实现克隆方法
std::unique_ptr<Shape> clone() const override {
return std::make_unique<Circle>(*this); // 调用自身的拷贝构造函数
}
void draw() const override {
std::cout << "Drawing Circle with radius " << radius << " and color " << (color ? *color : "N/A") << std::endl;
}
void setRadius(int r) { radius = r; }
void setColor(const std::string& c) { *color = c; }
};
// 具体原型类:矩形
class Rectangle : public Shape {
private:
int width;
int height;
// 假设矩形内部有一个简单的指针,需要深拷贝
int* id;
public:
Rectangle(int w = 0, int h = 0, int i = 0) : width(w), height(h), id(new int(i)) {
// std::cout << "Rectangle constructor: " << w << ", " << h << ", " << *id << std::endl;
}
// 拷贝构造函数:执行深拷贝
Rectangle(const Rectangle& other) : width(other.width), height(other.height) {
if (other.id) {
id = new int(*other.id); // 深拷贝id指向的数据
} else {
id = nullptr;
}
// std::cout << "Rectangle deep copy constructor called." << std::endl;
}
// 拷贝赋值运算符
Rectangle& operator=(const Rectangle& other) {
if (this != &other) {
width = other.width;
height = other.height;
delete id; // 释放旧资源
if (other.id) {
id = new int(*other.id); // 分配新资源并深拷贝
} else {
id = nullptr;
}
}
return *this;
}
~Rectangle() override {
delete id; // 释放动态分配的内存
// std::cout << "Rectangle destructor called." << std::endl;
}
// 实现克隆方法
std::unique_ptr<Shape> clone() const override {
return std::make_unique<Rectangle>(*this); // 调用自身的拷贝构造函数
}
void draw() const override {
std::cout << "Drawing Rectangle with width " << width << ", height " << height << " and ID " << (id ? *id : 0) << std::endl;
}
void setDimensions(int w, int h) { width = w; height = h; }
void setId(int i) { *id = i; }
};
// 客户端代码示例
// int main() {
// // 创建原型对象
// std::unique_ptr<Circle> originalCircle = std::make_unique<Circle>(10, "blue");
// std::unique_ptr<Rectangle> originalRectangle = std::make_unique<Rectangle>(20, 30, 101);
// originalCircle->draw();
// originalRectangle->draw();
// std::cout << "\nCloning objects...\n";
// // 克隆对象
// std::unique_ptr<Shape> clonedCircle = originalCircle->clone();
// std::unique_ptr<Shape> clonedRectangle = originalRectangle->clone();
// clonedCircle->draw();
// clonedRectangle->draw();
// std::cout << "\nModifying cloned objects (should not affect originals)...\n";
// // 修改克隆对象,验证深拷贝
// // 注意:clonedCircle和clonedRectangle是Shape类型,如果需要调用Circle/Rectangle特有的方法,需要dynamic_cast
// if (auto c = dynamic_cast<Circle*>(clonedCircle.get())) {
// c->setRadius(15);
// c->setColor("green");
// }
// if (auto r = dynamic_cast<Rectangle*>(clonedRectangle.get())) {
// r->setDimensions(25, 35);
// r->setId(102);
// }
// clonedCircle->draw();
// clonedRectangle->draw();
// std::cout << "\nOriginal objects after modification of clones (should remain unchanged):\n";
// originalCircle->draw();
// originalRectangle->draw();
// // 智能指针会自动管理内存,无需手动delete
// return 0;
// }这是个老生常谈的问题,但对于原型模式来说,它的重要性再怎么强调都不为过。浅拷贝(shallow copy)只是复制了对象本身的成员变量值,如果这些成员变量是指针,那么新旧对象会共享同一块内存区域。这听起来可能没什么,但一旦你修改了其中一个对象通过指针指向的数据,另一个对象的数据也会跟着变,这完全违背了我们克隆一个“独立”新对象的初衷。
立即学习“C++免费学习笔记(深入)”;
想象一下,你克隆了一个复杂的文档对象,如果只是浅拷贝,那么文档内部的图片、文本段落这些动态分配的内容,新旧文档可能都指向同一份数据。当你修改新文档里的某个图片,原文档里的图片也变了,这简直是灾难。深拷贝(deep copy)则不同,它会为所有动态分配的资源创建全新的副本。这意味着新对象拥有自己独立的数据,对新对象的任何修改都不会影响到原对象。原型模式的核心价值就在于提供一个“全新且独立”的副本,深拷贝是实现这一目标不可或缺的基石。没有深拷贝,原型模式就失去了它最吸引人的魔力,变成了一个潜在的bug制造机。

clone()
设计
clone()
virtual Shape* clone() const = 0;
delete
所以,我个人更倾向于使用智能指针,特别是
std::unique_ptr<Shape>
clone()
std::unique_ptr<Shape>
virtual std::unique_ptr<Shape> clone() const = 0;
std::unique_ptr
clone()
unique_ptr
unique_ptr
delete
当你的原型对象内部包含其他复杂对象,特别是那些本身也具有多态性的成员时,确保克隆的完整性会变得有点棘手。如果一个
Shape
Material
Material
PlasticMaterial
MetalMaterial
Shape
Material
Material
正确的做法是,如果
Shape
std::unique_ptr<Material>
clone()
Shape
clone()
例如,如果
Shape
std::unique_ptr<Material> material;
Shape
// Shape的拷贝构造函数片段
MyShape(const MyShape& other) : /* ...其他成员... */ {
if (other.material) {
material = other.material->clone(); // 递归调用material的clone()方法
} else {
material = nullptr;
}
}这意味着,你的
Material
virtual std::unique_ptr<Material> clone() const = 0;
Material
PlasticMaterial
MetalMaterial
Material
Material
clone()
以上就是怎样用C++实现原型模式 深拷贝与克隆接口的设计考量的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号