Pattern Comparison
The Builder and Abstract Factory patterns are similar in that they both look at construction at an abstract level. However, the Builder pattern is concerned with how a single object is made up by the different factories, whereas the Abstract Factory pattern is concerned with what products are made. The Builder pattern abstracts the algorithm for construction by including the concept of a director. The director is responsible for itemizing the steps and calls on builders to fulfill them. Directors do not have to conform to an interface.
A further elaboration on the theme of creating products is that
instead of the client explicitly declaring fields of type ProductA
and ProductB
, say, the Product
object the builder returns is actually
a list of parts, which can have different lengths and contents depending
on the director that was in charge at its creation.
A comparison of the two patterns is given in Table 6-1.
Table 6-1. Comparison of Abstract Factory and Builder patterns
Criteria | Abstract Factory | Builder |
---|---|---|
Client aggregates | A Factory and Products | A Director, Builders, and a Product |
Product creation is via | A factory | A director |
Product creation invokes | CreateProductA | Construct(builder) |
Factory/Builder returns | A specific product | A part of a product |
Product has | Specific properties | A list of parts |
Get C# 3.0 Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.