Composition not "Composition"

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

Deitel PJ, Deitel HM - Java How to Program 9th Edition.

This view is discussed in this section: Prefer composition over inheritance?

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

* this is a binary association,

* this is a whole / partial relationship,

* a part can be included in no more than one composite (whole) at a time, and

* if a composite (integral) is removed, all its component parts are "usually" removed with it.

Found at http://www.uml-diagrams.org/composition.html (in fact, Deitel presents UML examples following this idea in the same book, but didn't bother to explain the difference).

This view is discussed in this section:

What is the difference between association, aggregation and composition?

Okay, BACK TO CORRECT. And this introduces the problem of homonym concepts. For example: don't draw a UML model with compositional arrows to illustrate the first definition: in the UML, any association is composition using Deitels' of the first definition. Here are some aspects of my question that might help in getting the right answer:

How can I tell (and know) what composition we are speaking in?

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

May I say the former is Object Oriented Programming and the latter is Software Development / Modeling?

Is UML composition a model-only concept / jargon?

Is UML composition exclusive to UML? or also applied in the programming field?

How to avoid misunderstanding "which lineup are we talking about" in the team?

Please answer with links, proofs, this is not a philosophical / imaginary problem, this is a "volume" problem that I am trying to solve.

And it's not a question of "what is composition".

Edit: I think the difference is the x adjective verb: "compose" class (first def.) And "compound relation" (second def.).

+3


source to share


2 answers


I found it difficult to explain the difference between UML associations and implementation references without explaining at least a little what UML associations are and what they can do, so let's go.

Association and link

Let's start by looking at what a UML association and reference (association instance) are.

[11.5.3.1] An association defines the semantic relationships that can occur between typed instances.

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

Thus, the following is a valid implementation of a restricted association.

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

      

property

[9.5.3] When a property is owned by a Classifier other than an Association through its own attribute, it is an attribute of the classifier.

(Note: the class is a subclass of the classifier.)

Navigability

[11.5.3.1] A finite property of an Association belonging to a final class or being navigable as a Single Part of the Association indicates that the Association is navigable from opposite ends; otherwise, the Association is not navigable from opposite ends. Navigational ability means that the instances participating in links at runtime (Association instances) can be efficiently accessed from instances at the other ends of the Association. The exact mechanism by which such efficient access is achieved is implementation specific. If the end is not navigable, access from other ends may or may not be possible, and if so, it may be ineffective.

Why are these concepts relevant? Imagine the following example.

enter image description here

We can see that it brain

is a class attribute Head

(black dot means ownership of the opposite class) and that it is navigable (arrow). We can also see that it is Head

NOT an attribute brain

(the black dot β‡’ does not belong to the class Brain β‡’ is not a Brain attribute), however it is still navigable. This means that in UML, the property Head

belongs to the association itself.

The implementation can, for example, look like this (the association itself is represented by a tuple of two links (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 will hopefully begin to see, the UML association is not as simple a concept as the has-a relationship.

Composition

Allows you to add another piece of the composition.

[11.5.3.1] A binary association can represent a composite collection (ie, a whole / part relationship). Composition is represented by the attribute isComposite [9.9.17] The isComposite value is true only if the aggregation is composite.

When aggregating

  • none - Indicates that the property has no aggregation semantics.
  • shared - Indicates that the property has common aggregation semantics. The exact semantics of general aggregation vary by application domain and modeler.
  • composite - indicates that the property is being aggregated composite, i.e. a composite object is responsible for the existence and storage of configured objects

Again, we see that the UML association explicitly indicates concepts that are difficult to grasp from the implementation (for example, who is responsible for managing / destroying objects).

Model composition and object implementation composition



So from the above description, we can construct a more accurate description of what the implementation composition (is-relation) would be.

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

None of them, however, talk about how objects (composited in container, composer-composited) are related to each other, who is in charge of lifecycles, or whether the contained element is contained in the container.

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

enter image description here

So, if you call something compositional in programming, you can pretty much use any link / reference (or rather not inheritance), so the word itself isn't very useful.

In the UML, on the other hand, you are trying to capture all that kind of information about how objects relate to each other. Therefore, the focus here is on clarifying the conditions. So when you call something composition in the UML, you mean a very specific relationship in which the container is responsible for the lifecycle of the contained elements.

Implementing UML Associations

All these additional concepts of information mean that there is really no clear way how to implement associations. This makes sense because the implementation will depend on the target programming language or environment (for example, executable models where UML concepts are used as the final product).

As an example, I can recommend a document that describes how to implement a UML association in Java using concepts such as plurality, navigability, and visibility. Implementing UML Associations in Java .


Other subqueries

How can I tell (and know) what composition we are speaking in?

In context, or you can just ask (which is always good to do when you're not sure). Personally, I've heard that using composition "has a relationship" only when differentiating from inheritance; and the rest is in UML terms. But again, I'm in academia, so my opinion is biased.

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

Since the composition of the terms "programming" doesn't actually mean anything (just what it is β€” a), I would recommend drawing the line yourself and nudging others to use more precise terminology.

May I say the former is Object Oriented Programming and the latter is Software Development / Modeling?

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

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

No, you can use it in programming to mean the same thing as in the UML, but you may need to specify it more obviously. For example. "This class is composite for these classes because it manages their life cycle." The bottom line is to teach people to distinguish between regular and old - relationships and relationships that have more precise semantics.

How to avoid misunderstanding "which lineup are we talking about" in the team?

This is a very broad question that you can apply to any term you want to attach special meaning to (what even is software development?) And there is no better way. Have a common vocabulary (you probably already have a lot of specific terms in your domain), and guide people to use more accurate terminology.


the numbered quotes refer to sections in the UML 2.5 specification .

+2


source


To quote the UML 2.5 specification on page 110:

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

none : Indicates that the property has no aggregation semantics.

general . Indicates that the property has general aggregation semantics. The exact semantics of general aggregation vary by application domain and modeler.

composite . Indicates that the property is being aggregated composite, i.e. a composite object is responsible for the existence and storage of composited objects (see the definition of parts in 11.2.3).



Personally, I see how the notion of a composite aggregation refers to the lifetime of an object rather than a static relationship. A composite collection kills aggregate members when their parent dies. Nobody leaves it open. And general aggregation is a bastard that OMG shouldn't introduce at all since the semantics are domain dependent.

0


source







All Articles