Skip to content

Commit d6fcf4d

Browse files
committed
add Builder Pattern
1 parent d469b26 commit d6fcf4d

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

CreationalPatterns/Builder/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Builder Pattern
2+
3+
Builder produces different types and representations of an object using the same building process. Builder allows constructing complex object step by step.
4+
5+
## Problem
6+
7+
* How can a class create different representations of a complex object?
8+
* How can a class that includes creating a complex object simplified?
9+
* Constructor with lots of (optional) parameters is not convenient.
10+
* Constructor overloading with different parameters will be useless in most cases.
11+
* Creating subclasses with all combinations of the parameters will be tedious.
12+
13+
Creating and assembling the parts of a complex object within a class is inflexible and make it impossible to change the representation later independently from (without having to change) the class.
14+
15+
## Solution
16+
17+
* Encapsulate creating and assembling the parts of a complex object in a separate `Builder` object.
18+
* A class delegates object creation to a `Builder` object instead of creating the objects directly.
19+
20+
A class can delegate to different `Builder` objects to create different representations of a complex object.
21+
22+
![Real world example](https://sourcemaking.com/files/v2/content/patterns/Builder_example1.svg)
23+
24+
*Source*: SourceMaking.com
25+
26+
## Common Structure
27+
28+
![Common structure of Builder pattern](https://upload.wikimedia.org/wikipedia/commons/8/87/W3sDesign_Builder_Design_Pattern_UML.jpg)
29+
30+
* Builder
31+
* declares steps required to build a product
32+
* ConcreteBuilder
33+
* provides implementation for Builder. It is an object able to construct and assemble parts to build other objects.
34+
* Product
35+
* is an object created as result of construction.
36+
* Director
37+
* constructs products using a `Builder` object.
38+
39+
> Builder pattern does not require Director class. The separate director class is handy when you have several product variants that require different construction process. Director can encapsulate all that code inside a single class.
40+
41+
## Collaboration
42+
43+
![Sequence Diagram](img/sequence-diag.PNG)
44+
45+
* The client creates the Director object and configures it with the desired Builder object.
46+
* Director notifies the builder whenever a part of the product should be built.
47+
* Builder handles requests from the director and adds parts to the product.
48+
* The client retrieves the product from the builder.
49+
50+
## Benefits
51+
52+
* Different products can be built using the same code.
53+
* Encapsulate code for construction and representation
54+
* Isolates the complex construction code from a product's core business logic.
55+
* Provides control over steps of construction process.
56+
57+
## Drawbacks
58+
59+
* Requires a separate ConcreteBuilder for each different type of Product
60+
* Requires the builder classes to be mutable.
61+
62+
> Increases overall code complexity by creating multiple additional classes.
63+
64+
## Known uses
65+
66+
* StringBuilder - A mutable sequence of characters
67+
68+
## Example
69+
70+
**Definition**
71+
72+
**Usage**
73+
74+
## Comparison with other patterns
75+
76+
* Builders can produce products that do not belong to the same class hierarchy or interface. It is a key difference between the `Builder` and other creational patterns.
77+
78+
* **Abstract Factory** creates families of product objects (either simple or complex). Builder constructs a complex object step by step and returns the product as a final step, but the AbstractFactory returns the result immediately.
79+
80+
* **Composite** - Builder can be used to build a complex `Composite` tree step by step.
47.4 KB
Loading
49.2 KB
Loading

CreationalPatterns/CreationalPatterns.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
<None Include="FactoryMethod\README.md" />
7070
<None Include="Prototype\README.md" />
7171
</ItemGroup>
72-
<ItemGroup />
72+
<ItemGroup>
73+
<Folder Include="Builder\" />
74+
</ItemGroup>
7375
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7476
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7577
Other similar extension points exist, see Microsoft.Common.targets.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Design Patterns in C# / .NET
1111
| | Pattern |
1212
| ---|--- |
1313
|:heavy_check_mark: | [Abstract Factory](/CreationalPatterns/AbstractFactory/)|
14-
| | Builder|
14+
|| [Builder](/CreationalPatterns/Builder)|
1515
|:heavy_check_mark: | [Factory Method](/CreationalPatterns/FactoryMethod/) |
1616
| | Lazy Initialization
1717
|:heavy_check_mark: | [Prototype](/CreationalPatterns/Prototype) |

StructuralPatterns/StructuralPatterns.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
<Compile Include="Adapter\TestAdapter.cs" />
4848
<Compile Include="Adapter\TurkeyObjectAdapter.cs" />
4949
<Compile Include="Adapter\TurkeyClassAdapter.cs" />
50+
<Compile Include="Bridge\BasicRemote.cs" />
51+
<Compile Include="Bridge\IDevice.cs" />
52+
<Compile Include="Bridge\IRemote.cs" />
5053
<Compile Include="Composite\FileSystem.cs" />
5154
<Compile Include="Composite\TestComposite.cs" />
5255
<Compile Include="Program.cs" />
@@ -55,6 +58,7 @@
5558
<ItemGroup>
5659
<None Include="Adapter\README.md" />
5760
<None Include="App.config" />
61+
<None Include="Bridge\README.md" />
5862
<None Include="Composite\README.md" />
5963
<None Include="Decorator\README.md" />
6064
<None Include="doc\Adapter.cd" />

0 commit comments

Comments
 (0)