Warning: getimagesize(/home/spamerme/public_html/wiki/lib/tpl/default/images/logo.png) [function.getimagesize]: failed to open stream: No such file or directory in /home/spamerme/public_html/wiki/inc/template.php on line 1646
Software Development and Architecture Wiki

User Tools

Site Tools


oo_java_uml_association

UML Associations in Java

What is a UML Association ?

Associations are the least refined relationship between two classes and as such should only be used on domain models. When one class knows about the existence of another class and may address its interface directly those classes can be said to have an association. It means there is a communication path between the two classes. In a pure OO sense they can send messages to each other. In a implementation one class can call the public methods of the other class. In practice all Associations should be specialised into an Aggregation or a Composition for class diagrams.

What is Composition ?

Composition is a specialisation of a UML Association with a lifetime constraint. The lifetime of the composite part is constrained by the lifetime of the whole. The composite class is made up with one or more of the associated class. A Car is a composite of an Engine, a Body, a Gearbox, etc. The lifetime of all, and coupling is explicitly the same. e.g. Without these parts it is not really a car. Composition is therefore a rather static relationship, it cannot be changed during the lifetime of the Composite object and can be said to be immutable. Composition is an asymmetric relationship. It only makes sense to state a Car includes an Engine but makes no sense to say an Engine includes a Car.

Java implementation idioms

class Car {
       Engine engine = new Engine( this ) ;
}
 
class Engine {
       Car car = null ;
       public Engine(Car car) {
               this.car = car;
       }
}

Alternatively

Car car = new Car(new Engine()) ;
 
class Car {
       Engine engine = null ;
       Car(Engine engine) {
               this.engine = engine;
}
 
class Engine {
       Car car = null ;
       public Engine(Car car) {
               this.car = car;
       }
}

What is an Aggregation ?

Aggregation is also a specialisation of an Association, but without a lifetime constraint; Aggregation is therefore a dynamic relationship and mutable. One class is a container for instances of other classes which may be added or removed from the container as required. The lifetime of each contained class is independent of the container. e.g. A Car's relationship to its owner, driver & passengers is unrelated to the lifetime of each. Aggregation may also implies (but does not guarantee) uni-directional or asymmetric connectivity.

class Car {
       Driver driver;
       public setDriver( Driver driver) {
               this.driver = driver;
               driver.drive( this ) ;
               }
       }
 
class Driver {
       Car car;
       public drive(Car car) {
               this.car = car;
               }
       }

The aggregate instance may be polymorphic, so if Passenger and TaxiDriver extend Person, these could be added instead.

What is Dependency ?

Dependency is an artifact of compile or link time interface resolution and checking. A change to the interface of dependency target will result in a change to the dependant. Classes which appear as types in a method call are dependencies. Classes that are derived/inherited from an Associated class are dependencies. See also Dependency Injection

Are Associations a superset of Aggregation and Composition ?

In the UML meta-model; Associations are defined as a generalisation of Aggregation and Composition. Conversely Aggregation and Composition are therefore also specialisations of Association. In a practical sense Associations are often used during the domain analysis and will typically be refined into Aggregation and Composition as the design matures and class model emerges from the domain model.

What is a Qualified Association / Associative Class ?

A Qualified Association is where an Associative Class exists between the two Classes. In this example a Hashtable is used to hold the passengers and is an Associative Class.

class Car {
       Hashtable passengers;
       public setPassenger( Passenger passenger) {
               this.passengers.add( passenger ) ;
       }
}
oo_java_uml_association.txt ยท Last modified: 2013/07/01 08:20 by Martin Spamer