面向对象设计与设计模式实战指南

76 阅读2分钟

项目标题与描述

本项目是一个面向对象设计(OOD)和低层设计(LLD)的学习资源库,包含多种设计模式的完整C++实现。项目通过实际代码示例演示了适配器模式、桥接模式、装饰器模式等常用设计模式的应用,帮助开发者深入理解面向对象设计原则和模式实践。

功能特性

  • 适配器模式:实现LegacyGatewayAdapter将旧支付系统适配到新支付接口
  • 桥接模式:通过Renderer接口分离形状绘制与具体渲染实现
  • 装饰器模式:提供文本样式装饰功能,支持粗体、斜体和下划线组合
  • 建造者模式:HttpRequest.Builder提供灵活的HTTP请求构建方式
  • 责任链模式:实现请求处理链,包含认证、授权、限流等处理环节
  • 组合模式:File和Folder类实现文件系统结构的递归处理
  • 外观模式:DeploymentFacade简化复杂的应用部署流程
  • 工厂模式:NotificationFactory统一创建不同类型的通知对象
  • 享元模式:ShapeFactory实现圆形对象的共享和重用
  • 迭代器模式:提供统一的集合遍历接口
  • 中介者模式:FormMediator管理UI组件间的复杂交互逻辑

安装指南

系统要求

  • C++11或更高版本的编译器
  • CMake 3.10+(可选,用于构建项目)
  • 支持STL的标准C++库

安装步骤

  1. 克隆项目到本地:
git clone <repository-url>
cd lld-design-patterns
  1. 使用CMake构建项目:
mkdir build && cd build
cmake ..
make
  1. 运行特定模式的示例:
./adapter_pattern_demo
./bridge_pattern_demo
./decorator_pattern_demo

使用说明

适配器模式示例

// 使用新的支付处理器
PaymentProcessor* processor = new InHousePaymentProcessor();
processor->processPayment(100.0, "USD");

// 通过适配器使用旧支付系统
LegacyGateway* legacyGateway = new LegacyGateway();
PaymentProcessor* adapter = new LegacyGatewayAdapter(legacyGateway);
adapter->processPayment(200.0, "EUR");

桥接模式示例

VectorRenderer vectorRenderer;
RasterRenderer rasterRenderer;

// 矢量渲染的圆形
Circle vectorCircle(&vectorRenderer, 5.0f);
vectorCircle.draw();

// 光栅渲染的矩形
Rectangle rasterRectangle(&rasterRenderer, 6.0f, 2.0f);
rasterRectangle.draw();

装饰器模式示例

TextView* plain = new PlainTextView("Hello, world!");
TextView* bold = new BoldDecorator(plain);
TextView* italicBold = new ItalicDecorator(bold);
italicBold->render();

核心代码

适配器模式实现

// LegacyGatewayAdapter将旧支付系统适配到新接口
void LegacyGatewayAdapter::processPayment(double amount, const std::string& currency) {
    std::cout << "LegacyGatewayAdapter: Processing payment of " << amount << " " << currency << std::endl;
    legacyGateway->executeTransaction(amount, currency);
    std::cout << "LegacyGatewayAdapter: Payment processed successfully. Txn ID: " 
              << legacyGateway->getReferenceNumber() << std::endl;
}

桥接模式实现

// 形状基类与渲染器解耦
class Shape {
protected:
    Renderer* renderer;
public:
    explicit Shape(Renderer* renderer);
    virtual ~Shape() = default;
    virtual void draw() = 0;
};

// 具体形状实现
void Circle::draw() {
    renderer->renderCircle(radius);
}

装饰器模式实现

// 文本装饰器基类
class TextDecorator : public TextView {
protected:
    TextView* inner;
public:
    explicit TextDecorator(TextView* inner);
};

// 具体装饰器实现
void BoldDecorator::render() {
    std::cout << "<b>";
    inner->render();
    std::cout << "</b>";
}

建造者模式实现

// HTTP请求建造者
HttpRequest::Builder& HttpRequest::Builder::method(const std::string& m) {
    method = m.empty() ? "GET" : m;
    std::transform(method.begin(), method.end(), method.begin(), ::toupper);
    return *this;
}

HttpRequest HttpRequest::Builder::build() {
    if ((method == "POST" || method == "PUT") && body.empty()) {
        std::cout << "Warning: Building " << method << " request without a body" << std::endl;
    }
    return HttpRequest(*this);
}