建造者模式通过分离复杂对象的构建与表示,使同一构造过程可创建不同对象。包含Product(报告)、Builder(抽象构建接口)、ConcreteBuilder(如HtmlReportBuilder)和Director(指挥构建流程)。示例中用ReportDirector指导HtmlReportBuilder分步构建简易或完整HTML报告,最终输出带样式的报告内容。该模式适用于参数多、构造复杂、需灵活配置的场景,提升代码可读性与维护性,避免伸缩构造器反模式。

在C++中,建造者模式(Builder Pattern)用于分步构建复杂对象,尤其适用于对象构造过程复杂、参数多、可选配置多的情况。它将构造逻辑与表示分离,使得同样的构造过程可以创建不同的表示。
建造者模式的核心思想
将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。建造者模式通常包含以下几个角色:
- Product:要构建的复杂对象。
- Builder:抽象接口,定义构建各部分的方法。
- ConcreteBuilder:实现Builder接口,具体实现构建步骤。
- Director:指挥者,调用Builder来分步构建对象。
实现示例:构建一个网页报告
假设我们要构建一个复杂的报告对象,包含标题、正文、页脚、样式等部分,使用建造者模式可以分步设置这些内容。
本文档主要讲述的是SCA介绍及应用实例;SCA(Service Component Architecture)是针对SOA提出的一套服务体系构建框架协议,内部既融合了IOC的思想,同时又把面向对象的复用由代码复用上升到了业务模块组件复用,同时将服务接口,实现,部署,调用完全分离,通过配置的形式灵活的组装,绑定。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看
立即学习“C++免费学习笔记(深入)”;
#include#include #include // 产品类:报告 class Report { public: std::string title; std::string content; std::string footer; std::string cssStyle;
void show() const { std::cout zuojiankuohaophpcnzuojiankuohaophpcn "标题: " zuojiankuohaophpcnzuojiankuohaophpcn title zuojiankuohaophpcnzuojiankuohaophpcn "\n"; std::cout zuojiankuohaophpcnzuojiankuohaophpcn "内容: " zuojiankuohaophpcnzuojiankuohaophpcn content zuojiankuohaophpcnzuojiankuohaophpcn "\n"; std::cout zuojiankuohaophpcnzuojiankuohaophpcn "页脚: " zuojiankuohaophpcnzuojiankuohaophpcn footer zuojiankuohaophpcnzuojiankuohaophpcn "\n"; std::cout zuojiankuohaophpcnzuojiankuohaophpcn "样式: " zuojiankuohaophpcnzuojiankuohaophpcn cssStyle zuojiankuohaophpcnzuojiankuohaophpcn "\n"; }};
// 抽象建造者 class ReportBuilder { public: virtual ~ReportBuilder() = default; virtual void setTitle(const std::string& title) = 0; virtual void setContent(const std::string& content) = 0; virtual void setFooter(const std::string& footer) = 0; virtual void setStyle(const std::string& style) = 0; virtual std::unique_ptr
getReport() = 0; }; // 具体建造者:HTML报告 class HtmlReportBuilder : public ReportBuilder { std::unique_ptr
report; public: HtmlReportBuilder() { report = std::make_unique
(); } void setTitle(const std::string& title) override { report-youjiankuohaophpcntitle = "[HTML] " + title; } void setContent(const std::string& content) override { report-youjiankuohaophpcncontent = "zuojiankuohaophpcnpyoujiankuohaophpcn" + content + "zuojiankuohaophpcn/pyoujiankuohaophpcn"; } void setFooter(const std::string& footer) override { report-youjiankuohaophpcnfooter = "© " + footer; } void setStyle(const std::string& style) override { report-youjiankuohaophpcncssStyle = "font-family: " + style + "; color: blue;"; } std::unique_ptrzuojiankuohaophpcnReportyoujiankuohaophpcn getReport() override { return std::move(report); }};
// 指挥者 class ReportDirector { ReportBuilder* builder;
public: explicit ReportDirector(ReportBuilder* b) : builder(b) {}
void setBuilder(ReportBuilder* b) { builder = b; } // 构建简易报告 void makeSimpleReport() { builder-youjiankuohaophpcnsetTitle("简报"); builder-youjiankuohaophpcnsetContent("这是内容"); builder-youjiankuohaophpcnsetFooter("2024"); builder-youjiankuohaophpcnsetStyle("Arial"); } // 构建完整报告 void makeFullReport() { builder-youjiankuohaophpcnsetTitle("年度报告"); builder-youjiankuohaophpcnsetContent("详细数据分析..."); builder-youjiankuohaophpcnsetFooter("公司名称"); builder-youjiankuohaophpcnsetStyle("Times New Roman"); }};
使用建造者模式构建对象
客户端代码通过Director和Builder协作,分步构建对象。
立即学习“C++免费学习笔记(深入)”;
int main() { HtmlReportBuilder htmlBuilder; ReportDirector director(&htmlBuilder);// 构建简易HTML报告 director.makeSimpleReport(); auto report = htmlBuilder.getReport(); report-youjiankuohaophpcnshow(); std::cout zuojiankuohaophpcnzuojiankuohaophpcn "\n---\n\n"; // 构建完整报告 director.makeFullReport(); auto fullReport = htmlBuilder.getReport(); fullReport-youjiankuohaophpcnshow(); return 0;}
输出结果:
标题: [HTML] 简报 内容:这是内容
页脚: © 2024 样式: font-family: Arial; color: blue;标题: [HTML] 年度报告 内容:
详细数据分析...
页脚: © 公司名称 样式: font-family: Times New Roman; color: blue;优点与适用场景
建造者模式适合以下情况:
- 构造函数参数过多,尤其是存在多个可选参数时。
- 对象需要分阶段构建,且过程可变。
- 希望复用构建过程,创建不同表示的对象。
它提高了代码的可读性和可维护性,避免了“伸缩构造器反模式”(telescoping constructor)。
基本上就这些。









