C++ 设计模式综合实例

C++ 设计模式综合实例

下面是一个综合使用工厂模式、适配器、装饰器、代理和外观模式的完整C++示例。这个例子模拟一个图形处理系统。

1. 基础接口和类

#include <iostream>
#include <memory>
#include <vector>
#include <string>

// 图形接口
class Shape {
public:
    virtual ~Shape() = default;
    virtual void draw() const = 0;
    virtual std::string getName() const = 0;
};

// 具体图形类
class Circle : public Shape {
public:
    void draw() const override {
        std::cout << "Drawing Circle" << std::endl;
    }
    
    std::string getName() const override {
        return "Circle";
    }
};

class Rectangle : public Shape {
public:
    void draw() const override {
        std::cout << "Drawing Rectangle" << std::endl;
    }
    
    std::string getName() const override {
        return "Rectangle";
    }
};

class Triangle : public Shape {
public:
    void draw() const override {
        std::cout << "Drawing Triangle" << std::endl;
    }
    
    std::string getName() const override {
        return "Triangle";
    }
};

2. 工厂模式 - 创建对象

// 图形工厂
class ShapeFactory {
public:
    enum ShapeType { CIRCLE, RECTANGLE, TRIANGLE };
    
    static std::unique_ptr<Shape> createShape(ShapeType type) {
        switch(type) {
            case CIRCLE:    return std::make_unique<Circle>();
            case RECTANGLE: return std::make_unique<Rectangle>();
            case TRIANGLE:  return std::make_unique<Triangle>();
            default:        return nullptr;
        }
    }
};

3. 适配器模式 - 接口转换

// 第三方图形类(不兼容接口)
class LegacyGraphics {
public:
    void render(const std::string& shapeName) {
        std::cout << "Legacy rendering: " << shapeName << std::endl;
    }
};

// 适配器
class LegacyGraphicsAdapter : public Shape {
private:
    LegacyGraphics legacyGraphics;
    std::string name;
    
public:
    LegacyGraphicsAdapter(const std::string& name) : name(name) {}
    
    void draw() const override {
        legacyGraphics.render(name);
    }
    
    std::string getName() const override {
        return name;
    }
};

4. 装饰器模式 - 动态添加功能

// 基础装饰器
class ShapeDecorator : public Shape {
protected:
    std::unique_ptr<Shape> decoratedShape;
    
public:
    ShapeDecorator(std::unique_ptr<Shape> shape) 
        : decoratedShape(std::move(shape)) {}
    
    void draw() const override {
        decoratedShape->draw();
    }
    
    std::string getName() const override {
        return decoratedShape->getName();
    }
};

// 具体装饰器 - 添加颜色
class ColoredShape : public ShapeDecorator {
private:
    std::string color;
    
public:
    ColoredShape(std::unique_ptr<Shape> shape, const std::string& color)
        : ShapeDecorator(std::move(shape)), color(color) {}
    
    void draw() const override {
        std::cout << "Applying color: " << color << std::endl;
        ShapeDecorator::draw();
    }
    
    std::string getName() const override {
        return color + " " + ShapeDecorator::getName();
    }
};

// 具体装饰器 - 添加边框
class BorderedShape : public ShapeDecorator {
private:
    int borderWidth;
    
public:
    BorderedShape(std::unique_ptr<Shape> shape, int width)
        : ShapeDecorator(std::move(shape)), borderWidth(width) {}
    
    void draw() const override {
        ShapeDecorator::draw();
        std::cout << "Adding border with width: " << borderWidth << std::endl;
    }
    
    std::string getName() const override {
        return ShapeDecorator::getName() + " with border";
    }
};

5. 代理模式 - 控制访问

// 代理 - 延迟加载
class ShapeProxy : public Shape {
private:
    mutable std::unique_ptr<Shape> realShape;
    ShapeFactory::ShapeType type;
    
public:
    ShapeProxy(ShapeFactory::ShapeType type) : type(type) {}
    
    void draw() const override {
        if(!realShape) {
            std::cout << "Loading real shape..." << std::endl;
            realShape = ShapeFactory::createShape(type);
        }
        realShape->draw();
    }
    
    std::string getName() const override {
        if(!realShape) {
            return "Proxy for " + std::to_string(type);
        }
        return realShape->getName();
    }
};

6. 外观模式 - 简化复杂操作

// 图形系统外观
class GraphicsSystem {
private:
    std::vector<std::unique_ptr<Shape>> shapes;
    
public:
    void addShape(std::unique_ptr<Shape> shape) {
        shapes.push_back(std::move(shape));
    }
    
    void drawAll() const {
        std::cout << "\nDrawing all shapes:" << std::endl;
        for(const auto& shape : shapes) {
            shape->draw();
        }
    }
    
    void printAllNames() const {
        std::cout << "\nAll shape names:" << std::endl;
        for(const auto& shape : shapes) {
            std::cout << shape->getName() << std::endl;
        }
    }
    
    // 创建并添加一个装饰后的复杂形状
    void addComplexShape(ShapeFactory::ShapeType type, 
                        const std::string& color, 
                        int borderWidth) {
        auto shape = ShapeFactory::createShape(type);
        auto colored = std::make_unique<ColoredShape>(std::move(shape), color);
        auto bordered = std::make_unique<BorderedShape>(std::move(colored), borderWidth);
        shapes.push_back(std::move(bordered));
    }
};

7. 主函数 - 使用示例

int main() {
    GraphicsSystem system;
    
    // 使用工厂创建基本形状
    auto circle = ShapeFactory::createShape(ShapeFactory::CIRCLE);
    auto rectangle = ShapeFactory::createShape(ShapeFactory::RECTANGLE);
    
    // 使用适配器
    auto legacyAdapter = std::make_unique<LegacyGraphicsAdapter>("OldCircle");
    
    // 使用装饰器
    auto redTriangle = std::make_unique<ColoredShape>(
        ShapeFactory::createShape(ShapeFactory::TRIANGLE), "Red");
    
    auto borderedRedTriangle = std::make_unique<BorderedShape>(
        std::move(redTriangle), 5);
    
    // 使用代理
    auto proxy = std::make_unique<ShapeProxy>(ShapeFactory::RECTANGLE);
    
    // 添加到系统
    system.addShape(std::move(circle));
    system.addShape(std::move(rectangle));
    system.addShape(std::move(legacyAdapter));
    system.addShape(std::move(borderedRedTriangle));
    system.addShape(std::move(proxy));
    
    // 使用外观的便捷方法
    system.addComplexShape(ShapeFactory::CIRCLE, "Blue", 3);
    
    // 执行操作
    system.drawAll();
    system.printAllNames();
    
    return 0;
}

这个示例展示了:

  1. 工厂模式用于创建不同类型的形状对象

  2. 适配器模式使不兼容的LegacyGraphics类能够与系统一起工作

  3. 装饰器模式动态地为形状添加颜色和边框

  4. 代理模式实现延迟加载

  5. 外观模式提供简化的接口来管理复杂的图形系统

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值