|
| 1 | +--- |
| 2 | +title: GoFデザインパターン - ビルダー編 |
| 3 | +date: 2026-02-04 |
| 4 | +category: Coding |
| 5 | +description: GoFコレクションにおけるビルダーパターン実践ガイド:概念、使いどころ、C++の実装例、注意点と出典を実用的に解説します |
| 6 | +tags: [Coding, DesignPattern] |
| 7 | +recommended: true |
| 8 | +thumbnail: assets/img/ogp.png |
| 9 | +--- |
| 10 | + |
| 11 | +こんにちは!パン君です。 |
| 12 | + |
| 13 | +今回は GoF(Gang of Four)のデザインパターンのビルダー編になります。 |
| 14 | +サンプルコード(C++)、作り方、使うタイミング、注意点などを含めて実用的に解説します。 |
| 15 | + |
| 16 | +# はじめに |
| 17 | + |
| 18 | +ビルドパターンは複雑なオブジェクトの生成過程を分離して可読性・拡張性を高めることができます。 |
| 19 | + |
| 20 | +一次情報として Wikipedia の定義を引用します。 |
| 21 | + |
| 22 | +> "ビルダー設計パターンの目的は、複雑なオブジェクトの構築とその表現を分離することである。これにより、同じ構築プロセスで異なる表現を生成できる。" |
| 23 | +> (出典: [https://en.wikipedia.org/wiki/Builder_pattern](https://en.wikipedia.org/wiki/Builder_pattern)) |
| 24 | +
|
| 25 | +[!CARD](https://en.wikipedia.org/wiki/Builder_pattern) |
| 26 | + |
| 27 | +## 構造(要素) |
| 28 | + |
| 29 | +典型的な構成は次の通りです。 |
| 30 | + |
| 31 | +- `Product`:最終的に生成される複雑なオブジェクト |
| 32 | +- `Builder` : `buildPartX()` のようなステップを定義し、`getResult()` を持つ |
| 33 | +- `ConcreteBuilder`:`Builder` を実装し、内部に可変な部品を持ち最終的な `Product` を返す |
| 34 | +- `Director`:構築の手順 を持ち、`Builder` に構築命令を出す。小さなケースでは Director を省くことも多い |
| 35 | + |
| 36 | +## C++ 実装例 |
| 37 | + |
| 38 | +以下は最小限の C++ 例です。`Director` を使ったパターンの典型例です。 |
| 39 | + |
| 40 | +```cpp |
| 41 | +// Product.h -- 生成対象 |
| 42 | +#pragma once |
| 43 | +#include <string> |
| 44 | + |
| 45 | +struct Product { |
| 46 | + std::string partA; |
| 47 | + std::string partB; |
| 48 | + std::string partC; |
| 49 | +}; |
| 50 | + |
| 51 | +// Builder.h -- 抽象ビルダー |
| 52 | +#pragma once |
| 53 | +#include <memory> |
| 54 | +class Builder { |
| 55 | +public: |
| 56 | + virtual ~Builder() = default; |
| 57 | + virtual void BuildPartA() = 0; |
| 58 | + virtual void BuildPartB() = 0; |
| 59 | + virtual void BuildPartC() = 0; |
| 60 | + virtual std::unique_ptr<Product> GetResult() = 0; |
| 61 | +}; |
| 62 | + |
| 63 | +// ConcreteBuilder.h |
| 64 | +#pragma once |
| 65 | +#include "Builder.h" |
| 66 | +#include "Product.h" |
| 67 | +#include <memory> |
| 68 | + |
| 69 | +class ConcreteBuilder : public Builder { |
| 70 | +private: |
| 71 | + std::unique_ptr<Product> product_; |
| 72 | +public: |
| 73 | + ConcreteBuilder() : product_(std::make_unique<Product>()) {} |
| 74 | + void BuildPartA() override { product_->partA = "Engine"; } |
| 75 | + void BuildPartB() override { product_->partB = "Wheels"; } |
| 76 | + void BuildPartC() override { product_->partC = "Body"; } |
| 77 | + std::unique_ptr<Product> GetResult() override { |
| 78 | + return std::move(product_); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +// Director.h |
| 83 | +#pragma once |
| 84 | +#include "Builder.h" |
| 85 | + |
| 86 | +class Director { |
| 87 | +public: |
| 88 | + void ConstructSportsCar(Builder& builder) { |
| 89 | + builder.BuildPartA(); |
| 90 | + builder.BuildPartB(); |
| 91 | + builder.BuildPartC(); |
| 92 | + } |
| 93 | + void ConstructToyCar(Builder& builder) { |
| 94 | + builder.BuildPartB(); |
| 95 | + // Toy car doesn't need engine |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +// main.cpp |
| 100 | +#include <iostream> |
| 101 | +int main() { |
| 102 | + ConcreteBuilder builder; |
| 103 | + Director director; |
| 104 | + director.ConstructSportsCar(builder); |
| 105 | + auto product = builder.GetResult(); |
| 106 | + std::cout << "Product parts: " << product->partA << ", " << product->partB << ", " << product->partC << "\n"; |
| 107 | +} |
| 108 | +``` |
| 109 | +
|
| 110 | +この実装のポイントは 構築手順 と 具体的構築 が分離されていることです。 |
| 111 | +ConcreteBuilder を差し替えるだけで多様性の実現が図れます。 |
| 112 | +
|
| 113 | +## いつ使うか |
| 114 | +
|
| 115 | +**ビルダーを検討する代表ケース** |
| 116 | +- コンストラクタのパラメータが多く、ほとんどがオプションである場合(名前付き引数やファクトリでも代替可) |
| 117 | +- 段階的または複雑な初期化が必要で、処理手順を分離したい場合(テスト用に段階的に検証できる) |
| 118 | +- 同じ構築手順で異なる表現(複数の `ConcreteBuilder`)を生成したい場合 |
| 119 | +- 不変オブジェクト(immutable)の生成を読みやすくしたい場合(Builder が可変の中間表現を組み立て最終生成で不変オブジェクトを返す) |
| 120 | +
|
| 121 | +**代替手段** |
| 122 | +- 言語がサポートする named / optional parameters(簡潔なケース) |
| 123 | +- `Factory Method` / `Abstract Factory`(生成の責務をサブクラスやグループで管理したい場合) |
| 124 | +- コンストラクタの段階的補助関数(簡単なケースで十分なことが多い) |
| 125 | +
|
| 126 | +## まとめ |
| 127 | +
|
| 128 | +Builder は 構築手順と表現の分離 を目的とした強力なパターンで、複雑な初期化・可変な表現を扱う場面で有効です。 |
| 129 | +しかし Builder はミュータブル性や DI との相性、テスト設計など考慮すべきトレードオフがあります。 |
| 130 | +導入前に影響範囲(生成元・シリアライズ・テスト)をした方がいいです。 |
| 131 | +
|
| 132 | +参考・出典を記します |
| 133 | +
|
| 134 | +[!CARD](https://en.wikipedia.org/wiki/Builder_pattern) |
| 135 | +
|
| 136 | +それでは、次回は別の GoF パターンについても解説していきます。 |
| 137 | +この記事で提示したコードは学習目的に簡潔化しています。 |
| 138 | +実プロダクションで採用する際は要件に合わせて十分に検討してください |
| 139 | +
|
| 140 | +以上、パン君でした! |
0 commit comments