Aggregation [ 'HAS-A' Relationship ]
class StereoSystem {
private boolean state ;
StereoSystem() {}
StereoSystem(boolean state) {
this.state = state ;
System.out.println("Stereo System State: " + (state == true ? "On!" : "Off!")) ;
}
}
class Car {
private StereoSystem s ;
Car() {}
Car(String name, StereoSystem s) {
this.s = s ;
}
public static void main(String[] args) {
StereoSystem ss = new StereoSystem(true) ; // true(System is ON.) or false (System is OFF)
Car c = new Car("BMW", ss) ;
}
}
In UML Aggregation is Expressed as
Composition [ 'Contains' Relationship ]
import java.util.Date ;
class Piston {
private Date pistonDate ;
Piston() {
pistonDate = new Date() ;
System.out.println("Manufactured Date :: " + pistonDate) ;
}
}
class Engine {
private Piston piston ;
Engine() {
piston = new Piston() ;
}
public static void main(String[] args) {
Engine engine = new Engine() ;
}
}
In UML Composition is Expressed as
I originally posted this here: http://www.coderanch.com/t/443002/java/java/Java-Coding-UML-Aggregation-Composition
Hope this Helps! Please write your comments it will help me improve.
Nice illustration.
ReplyDelete