3

In the Design Patterns: Elements of Reusable Object Oriented Software, the Gang of Four present the following canonical form for the Builder pattern:

enter image description here

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 a Color 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 its Points 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-of Polygon 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 with Color
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:

enter image description here

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?

16
  • 1
    I'm a little rusty on class diagrams in UML but I recall an open diamond base meant the object can exist separate from the object. A solid diamond base was for objects that live only as part of the parent.
    – JimmyJames
    Apr 12 at 18:26
  • 1
    To expand on what JimmyJames said, the solid or open diamond shape implies ownership of memory. The solid diamond means true composition: the memory allocated for one object is sufficient to hold all of the memory necessary for the "child" object. If memory for the "parent" object is reclaimed, the "child" object is also reclaimed. The open diamond implies one object holds a pointer to another object, and that both objects can live independently of one another. Apr 12 at 18:48
  • 2
    @user32882 I understand that but what I'm saying is that I don't see anywhere in the description of the diamond that it says the references can't be shared. The following sentence which mentions sharing is about associations. The fact that it follows directly does give the impression that aggregate references can't be shared but it doesn't actually say that. Furthermore, they use the term 'aggregation' which is well-defined and allows for sharing of references. Hence my assessment: sloppy.
    – JimmyJames
    Apr 12 at 19:18
  • 1
    On page 22 of my copy it says "Aggregation implies that an aggregate object and it's owner have identical lifetimes." The term 'implies' is a little weak but this would tend to agree with your assertion. I therefore have to agree that the sequence diagram you point to is inconsistent with this definition.
    – JimmyJames
    Apr 13 at 14:26
  • 1
    @user32882 That's one of many reasons why ancient books such as GoF are best left alone except for historical interest rather than treated as an informative source about modern software engineering - a great deal has changed in the meantime, and much of its content of questionable relevance in the modern world. Software Engineering was a younger and immature discipline 20 years ago; indeed the way that the world relies upon and thinks about technology has evolved greatly. Much of what might once have been conventional wisdom back then is often viewed as misguided or wrong today. Apr 14 at 11:46

1 Answer 1

4

The GOF is pre-UML. It uses a variant of OMT, a notation invented by J.Rumbaugh, before it was merged into UML:

  • The current UML terminology is "association". OMT made a subtle difference between bidirectional "associations" (see footnote, GOF Annex B) and unidirectional "acquaintances" (implemented with reference). In modern UML, both would be associations. The latter could be expressed with navigable associations, although UML is implementation neutral.
  • Moreover, although OMT and earlier version of UML both used the white diamond, to represent part-whole relationships, J.Rumbaugh himself called this shared aggregation thing a "modeling placebo". Since UML 2.0, UML has kept the white diamond, but no longer defines any semantic for it. So, white diamond or no diamond, end up having the same semantic in UML, and the simpler notation should be preferred.

The white diamond, the so called shared aggregation, does not imply ownership of the elements at the other end (not in OMT and not in UML): elements can perfectly outlive their aggregate. The lifecycle limitation is a characteristic of composition (more precisely, composite aggregation, which implies an exclusive ownership), which is represented with a black diamond.


Edit: additional information on OMT

To avoid any loss in translation, I acquired the legacy original OMT book. Rumbaugh uses the terms "association" and "aggregation" (no "acquaintance") and confirms my previous conclusions:

Aggregation is a special form of association, not an independent concept. (...) If the two objects are tightly bound by a part-whole relationship, it's an aggregation. If the two objects are usually considered as independent, even though they may be often linked, it is an association. (...) The decision to use aggregation is a matter of judgement and often arbitrary. (...)

3
  • Thanks. It sounds like OMT and UML <2.0 used a white diamond to represent composition and no diamond to represent association. UML2.0 merges the two to represent association, and adds a new black diamond symbol to represent composition. Would that be correct? Also your answer doesn't address the class diagram for the Builder pattern. If OMT was used, then a white diamond would have represented composition back then. This is contradicted by the sequence diagram which clearly shows that the lifetime of the Builder must be independent from that of the Director. Is the class diagram wrong?
    – user32882
    Apr 14 at 13:58
  • 1
    Not quite: UML had black diamond since the start to represent composition. The term compostion is btw ambiguous, as it may refer to composite aggregation, and object composition. Object composition is one way to implement both aggregation and association.
    – Christophe
    Apr 14 at 14:29
  • Moreover, the part-of relation holding a reference to a part does not imply lifecycle management as composit aggregation in UML. They used this notation, as many (still) use aggregation in UML, and since the reference is injected at construction, the class diagram is correct with the semantics described
    – Christophe
    Apr 14 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.