atorres
atorres

Reputation: 402

Composition is not "Composition"

Composition: A class can have references to objects of other classes as members. This is called composition and is sometimes referred to as a has-a relationship.

By Deitel P.J., Deitel H.M. - Java How to Program 9th Edition.

This viewpoint is discussed in this topic: Prefer composition over inheritance?

Composition: Composite aggregation (composition) is a "strong" form of aggregation with the following characteristics:

*it is binary association,

*it is a whole/part relationship,

*a part could be included in at most one composite (whole) at a time, and

*if a composite (whole) is deleted, all of its composite parts are "normally" deleted with it.

Found on http://www.uml-diagrams.org/composition.html (actually, Deitel presents UML examples following this idea, in the same book, but did not bother to explain the difference).

This viewpoint is discussed in this topic:

What is the difference between association, aggregation and composition?

Fine, BOTH ARE CORRECT. And this introduces the problem of homonym concepts. For instance: don't draw a UML model with composition arrows to exemplify the first definition: In UML, any association is a composition by Deitels' the first definition. Here are some aspects of my question that may help in the correct answer:

How I can say (and know) which composition are we talking about?

Where we draw the line between the two definitions (in contextual terms)?

Can I say that the first is object oriented programming and the second is software engineering/modeling?

Is the UML composition a model-only concept/jargon?

Is the UML composition an UML exclusive thing? or is also applied in the programming field?

How to avoid miscommunication of "what composition are we talking about" in a team?

Please, answer with references, evidences, it is not a philosophical/opinion problem, it is a "scope" problem that I´m trying to address.

And it is not "what is composition" question.

Edit: I´m thinking if the distinction is verb x adjective: "to compose" a class (first def.) and "a composite relation" (second def.).

Upvotes: 4

Views: 538

Answers (2)

Peter Uhnak
Peter Uhnak

Reputation: 10197

I found it hard to explain the difference between UML association and implementation references without explaining at least a little bit what UML associations actually are, and what they can do, so here we go.

Association & Link

Lets start by looking at what a UML Association and a link (Association's instance) are.

[11.5.3.1] An Association specifies a semantic relationship that can occur between typed instances.

[11.8.1.1] A link is a tuple of values that refer to typed objects. An Association classifies a set of links, each of which is an instance of the Association. Each value in the link refers to an instance of the type of the corresponding end of the Association.

So the following is a valid implementation of a limited association.

class Brain { }
class Head { }
a = new Brain;
b = new Head;
link = (new Array).add(a).add(b);

Ownership

[9.5.3] When a Property is owned by a Classifier other than an Association via ownedAttribute, then it represents an attribute of the Classifier.

(Note: Class is a subclass of a Classifier.)

Navigability

[11.5.3.1] An end Property of an Association that is owned by an end Class or that is a navigableOwnedEnd of the Association indicates that the Association is navigable from the opposite ends; otherwise, the Association is not navigable from the opposite ends. Navigability means that instances participating in links at runtime (instances of an Association) can be accessed efficiently from instances at the other ends of the Association. The precise mechanism by which such efficient access is achieved is implementation specific. If an end is not navigable, access from the other ends may or may not be possible, and if it is, it might not be efficient.

Why are those concepts relevant? Imagine the following example.

enter image description here

We see that brain is an attribute of Head class (the black dot signifies ownership by the opposite Class), and that it is navigable (the arrow). We also see that head is NOT an attribute of Brain (no black dot ⇒ not owned by the Brain class ⇒ not an attribute of Brain), however it is still navigable. This means that in UML the head Property is held by the association itself.

The implementation could, for example, look like this (the association itself is represented by a tuple of two references (see link description earlier)).

class Head {
  public Brain brain;
}

class Brain {
}

h = new Head;
b = new Brain;
h.brain = b;
link = (new Array).add(h).add(b);

So as you hopefully start to see, UML association is not such a simple concept as a has-a relationship.

Composition

Lets add another piece, composition.

[11.5.3.1] A binary Association may represent a composite aggregation (i.e., a whole/part relationship). Composition is represented by the isComposite attribute [9.9.17] The value of isComposite is true only if aggregation is composite.

With the aggregation being

  • none - Indicates that the Property has no aggregation semantics.
  • shared - Indicates that the Property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.
  • composite -- Indicates that the Property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects

Again we see, that a UML association is explicitly specifying concepts that are hard to perceive from implementation (e.g. who is responsible for object management/destruction).

Model Composition vs Object Implementation Composition

So from the description above we can construct a more precise description of what an implementation composition (has-a relationship) would be.

[Deteils] Composition: A class can have references to objects of other classes as members. This is called composition and is sometimes referred to as a has-a relationship. McConnell [Code Complete 2, 6.3] also refers to has-a relationship as a Containment.

Neither of them however talk about HOW the objects (container-contained, composer-composite) are related to one another, who is responsible for lifecycles, or whether the contained element knows about the container.

So just by saying that objects have a has-a relationship (and call it composition), you could actually mean any of these (and several more)

enter image description here

So if you call something composition in programming, you can mean pretty much any relationship/reference (or rather not an inheritance), so the word by itself is not very useful.

In UML on the other hand you are trying to capture all such information about how the objects are related to one another. Therefore there's a focus on giving terms a more precise meaning. So when you call something composition in UML you have in mind a very specific has-a relationship, where the container is responsible for the lifecycle of the contained items.

Implementation of UML associations

All those extra concepts information mean that there is really no precise way how to even implement associations. This makes sense as the implementation would depend on the target programming language or environment (e.g. executable models, where the UML concepts are used as the final product).

As an example I can recommend a paper describing UML association implementation in Java with enforced concepts such as multiplicity, navigability, and visibility Implementing UML Associations in Java.


More subquestions

How I can say (and know) which composition are we talking about?

By context, or you can just ask (which is always a good thing to do when unsure). Personally I've heard the use of composition as "has-a relationship" only when differentiating from inheritance; and in the rest in terms of UML. But then again I am in academia, so my view is biased.

Where we draw the line between the two definitions (in contextual terms)?

As the "programming" term composition doesn't actually mean anything (only that it is has-a), I'd recommend drawing the line yourself and pushing others to use more precise terminology.

Can I say that the first is object oriented programming and the second is software engineering/modeling?

More or less, with all the nuances mentioned in this answer.

Is the UML composition a model-only concept/jargon? Is the UML composition an UML exclusive thing? or is also applied in the programming field?

No, you can use it in programming to mean the same thing as it means in UML, but you might need to state it more obviously. E.g. "This class is a composite for those classes, because it manages their lifecycle.". The point is to teach people to differentiate between regular-old has-a relationships, and relationships that have more precise semantics.

How to avoid miscommunication of "what composition are we talking about" in a team?

This is a very broad question that you could apply to any term to which you want attach special meaning (what even is software engineering?), and there is no best way. Have a team-shared vocabulary (you are probably already having a lots of specific terms in your domain), and guide people to use more precise terminology.


numbered quotes refers to sections in UML 2.5 Specifications.

Upvotes: 2

qwerty_so
qwerty_so

Reputation: 36295

To cite the UML 2.5 specification on page 110:

Sometimes a Property is used to model circumstances in which one instance is used to group together a set of instances; this is called aggregation. To represent such circumstances, a Property has an aggregation property, of type AggregationKind; the instance representing the whole group is classified by the owner of the Property, and the instances representing the grouped individuals are classified by the type of the Property. AggregationKind is an enumeration with the following literal values:

none: Indicates that the Property has no aggregation semantics.

shared: Indicates that the Property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.

composite: Indicates that the Property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects (see the definition of parts in 11.2.3).

Personally I see it the way that notion of a composite aggregation is about object lifetime, not about static relation. A composite aggregation kills aggregate members when their parent dies. None leaves this open. And shared aggregation is a bastard that OMG should not have introduced at all since it's semantics is domain dependent.

Upvotes: 1

Related Questions