In the Design Patterns: Elements of Reusable Object Oriented Software, the Gang of Four present the following canonical form for the Builder pattern:
In Appendix B the following is mentioned regarding notation:
Slanted type indicates that the class or operation is abstract
An object reference representing a part-of or aggregation relationship is indicated by an arrowheaded line with a diamond at the base
An arrowheaded line without the diamond denotes acquaintance (e.g., a
LineShape
keeps a reference to aColor
object, which other shapes may share)
Martin Fowler's UML Distilled explains that composition is defined by two aspects:
- A composite should basically own its components. That is, deleting a
Polygon
object should ensure all of itsPoints
get deleted as well. - A composite should not share the objects it owns with other objects
Fowler uses the term "association" instead of "acquaintance". Regardless of which term is used, the idea of an associated object being shared by several other objects remains. Fowler goes on to state:
Aggregation is strictly meaningless; as a result, I recommend that you ignore it in your own diagrams
If we maintain the terminology "part-of" and "acquaintance" from the Gang of Four, this is what we are left with:
- Part-of relationship:
- Denoted by an arrowheaded line with a diamond at the base
Point
is a part-ofPolygon
in the snippet below:
class Polygon
{
private Point m_point;
public Polygon()
{
m_point = new Point();
}
}
- Acquaintance
- Denoted by an arrowheaded line without a diamond at the base
- In the following snippet,
LineShape
is acquainted withColor
class LineShape
{
private Color m_color;
public LineShape(Color color)
{
m_color = color;
}
}
Now, the GOF's diagram for the Builder pattern shows a part-of relationship between the Director
and the Builder
. However, as shown by the sequence diagram below, they also have an independent Client
create separate instances of a Builder
and Director
:
Because the Client
must send requests to both a Director
instance as well as a ConcreteBuilder
instance, it would have to instantiate both separately as shown below:
ConcreteBuilder aConcreteBuilder = new ConcreteBuilder();
Director aDirector = new Director(aConcreteBuilder);
aDirector.Construct();
Product product = aConcreteBuilder.GetResult();
This would imply that aConcreteBuilder
can outlive aDirector
and thus that they have a part-of instead of acquaintance relationship.
Is this a mistake? Or am I missing something?