C++中的原型模式

作者 : admin 本文共3606个字,预计阅读时间需要10分钟 发布时间: 2024-06-11 共1人阅读

目录

原型模式(Prototype Pattern)

实际应用

克隆形状对象

克隆计算机对象

克隆文档对象

总结


原型模式(Prototype Pattern)

原型模式是一种创建型设计模式,它允许你通过复制现有对象来创建新的对象,而不是通过类实例化来创建对象。这种模式在开发时需要大量类似对象的情况下非常有用。原型模式的核心是一个具有克隆方法的接口,通过实现该接口,不同的对象可以被复制。

实际应用

C++中,原型模式通常使用一个基类(原型接口)和具体实现类来实现。基类包含一个纯虚拟的克隆方法,每个具体类实现该方法以返回自己的克隆。

克隆形状对象

假设我们有一个`Shape`基类和一些具体的形状类,如`Circle`和`Rectangle`。

#include 
#include 
#include 

// Shape基类,包含一个纯虚拟的克隆方法
class Shape {
public:
    virtual ~Shape() = default;
    virtual std::unique_ptr clone() const = 0;
    virtual void draw() const = 0;
};

// Circle类,继承自Shape并实现克隆方法
class Circle : public Shape {
private:
    int radius;
public:
    Circle(int r) : radius(r) {}
    std::unique_ptr clone() const override {
        return std::make_unique(*this);
    }
    void draw() const override {
        std::cout << "Drawing a Circle with radius " << radius << "
";
    }
};

// Rectangle类,继承自Shape并实现克隆方法
class Rectangle : public Shape {
private:
    int width, height;
public:
    Rectangle(int w, int h) : width(w), height(h) {}
    std::unique_ptr clone() const override {
        return std::make_unique(*this);
    }
    void draw() const override {
        std::cout << "Drawing a Rectangle with width " << width << " and height " << height << "
";
    }
};

int main() {
    // 创建原型对象
    std::unordered_map<std::string, std::unique_ptr> prototypes;
    prototypes["circle"] = std::make_unique(10);
    prototypes["rectangle"] = std::make_unique(20, 30);

    // 克隆对象
    std::unique_ptr shape1 = prototypes["circle"]->clone();
    std::unique_ptr shape2 = prototypes["rectangle"]->clone();

    // 绘制克隆的对象
    shape1->draw();
    shape2->draw();

    return 0;
}

克隆计算机对象

假设我们有一个`Computer`基类和一些具体的计算机类,如`Laptop`和`Desktop`。

#include 
#include 
#include 

// Computer基类,包含一个纯虚拟的克隆方法
class Computer {
public:
    virtual ~Computer() = default;
    virtual std::unique_ptr clone() const = 0;
    virtual void display() const = 0;
};

// Laptop类,继承自Computer并实现克隆方法
class Laptop : public Computer {
private:
    int batteryLife;
public:
    Laptop(int battery) : batteryLife(battery) {}
    std::unique_ptr clone() const override {
        return std::make_unique(*this);
    }
    void display() const override {
        std::cout << "Displaying a Laptop with battery life " << batteryLife << " hours
";
    }
};

// Desktop类,继承自Computer并实现克隆方法
class Desktop : public Computer {
private:
    bool hasMonitor;
public:
    Desktop(bool monitor) : hasMonitor(monitor) {}
    std::unique_ptr clone() const override {
        return std::make_unique(*this);
    }
    void display() const override {
        std::cout << "Displaying a Desktop " << (hasMonitor ? "with" : "without") << " monitor
";
    }
};

int main() {
    // 创建原型对象
    std::unordered_map<std::string, std::unique_ptr> prototypes;
    prototypes["laptop"] = std::make_unique(8);
    prototypes["desktop"] = std::make_unique(true);

    // 克隆对象
    std::unique_ptr comp1 = prototypes["laptop"]->clone();
    std::unique_ptr comp2 = prototypes["desktop"]->clone();

    // 显示克隆的对象
    comp1->display();
    comp2->display();

    return 0;
}

克隆文档对象

假设我们有一个`Document`基类和一些具体的文档类,如`WordDocument`和`PDFDocument`。

#include 
#include 
#include 

// Document基类,包含一个纯虚拟的克隆方法
class Document {
public:
    virtual ~Document() = default;
    virtual std::unique_ptr clone() const = 0;
    virtual void print() const = 0;
};

// WordDocument类,继承自Document并实现克隆方法
class WordDocument : public Document {
private:
    int wordCount;
public:
    WordDocument(int words) : wordCount(words) {}
    std::unique_ptr clone() const override {
        return std::make_unique(*this);
    }
    void print() const override {
        std::cout << "Printing a Word Document with word count " << wordCount << "
";
    }
};

// PDFDocument类,继承自Document并实现克隆方法
class PDFDocument : public Document {
private:
    int pageCount;
public:
    PDFDocument(int pages) : pageCount(pages) {}
    std::unique_ptr clone() const override {
        return std::make_unique(*this);
    }
    void print() const override {
        std::cout << "Printing a PDF Document with page count " << pageCount << "
";
    }
};

int main() {
    // 创建原型对象
    std::unordered_map<std::string, std::unique_ptr> prototypes;
    prototypes["word"] = std::make_unique(5000);
    prototypes["pdf"] = std::make_unique(100);

    // 克隆对象
    std::unique_ptr doc1 = prototypes["word"]->clone();
    std::unique_ptr doc2 = prototypes["pdf"]->clone();

    // 打印克隆的对象
    doc1->print();
    doc2->print();

    return 0;
}

总结

每个原型类实现自己的克隆方法,从而确保了对象的正确复制。

本站无任何商业行为
个人在线分享 » C++中的原型模式
E-->