Sequence Diagrams


Aggregation Example Code

At this point it will prove helpful to you to see a few short example programs that implement simple and composite aggregation before attempting a more complex example. Let’s start with a simple aggregation.

Simple Aggregation Example

Figure 10-4 gives a UML diagram showing a simple aggregate class named A that uses the services of a part class named B.

image from book
Figure 10-4: Simple Aggregation Example

Class A has one attribute, its_b, which is a reference to an object of class B. A reference to a B object is passed into an object of class A via a constructor argument when an object of class A is created. Class A has another method named makeContainedObjectSayHi() which returns void.

Class B has no attributes, a constructor method, and another method named sayHi(). Examples 10.1 and 10.2 give the code for these two classes.

Example 10.1: A.java

image from book
 1     public class A { 2        private B its_b = null; 3        public A(B b_ref){ 4          its_b = b_ref; 5          System.out.println("A object created!"); 6        } 7 8       public void  makeContainedObjectSayHi(){ 9           its_b.sayHi(); 10      } 11     }
image from book

Example 10.2: B.java

image from book
 1     public class B { 2       public B(){ 3         System.out.println("B object created!"); 4       } 5 6       public void   sayHi(){ 7         System.out.println("Hi!"); 8       } 9     }
image from book

Referring to example 10.1 — the its_b reference variable is declared on line 2 and initialized to null. The constructor takes a reference to a B object and assigns it to the its_b variable. The its_b variable is then used on line 9 in the makeContainedObjectSayHi() method to call its sayHi() method.

The only thing to note in example 10.2 is the sayHi() method which starts on line 6. It just prints a simple message to the console. Example 10.3 gives a short program called TestDriver that is used to test classes A and B and illustrate simple aggregation.

Example 10.3: TestDriver.java

image from book
 1     public class TestDriver { 2      public static void main(String[] args){ 3         B b = new B(); 4         A a = new A(b); 5         a.makeContainedObjectSayHi(); 6       } 7     }
image from book

Referring to example 10.3 — a B object is first created on line 3. The reference b is used as an argument to the A() constructor. In this manner an A object gets a reference to a B object. On line 5 the reference a is used to call the makeContainedObjectSayHi() method. The results of running this program are shown in figure 10-5.

image from book
Figure 10-5: Results of Running Example 10.3

Composite Aggregation Example

Figure 10-6 gives a UML diagram that illustrates composite aggregation between classes A and B.

image from book
Figure 10-6: Composite Aggregation Example

Referring to figure 10-6 — class A still has an attribute named its_b and a method named makeContainedObject-SayHi(), however, the A() constructor has no parameters. Class B is exactly the same here as it was in the previous example. The source code for both these classes is given in examples 10.4 and 10.5.

Example 10.4: A.java

image from book
 1     public class A { 2        private B its_b = null; 3        public A(){ 4         its_b = new B(); 5         System.out.println("A object created!"); } 6 7       public void makeContainedObjectSayHi(){ 8           its_b.sayHi(); 9       } 10    }
image from book

Example 10.5: B.java

image from book
 1     public class B { 2       public B(){ 3         System.out.println("B object created!"); 4       } 5 6       public void   sayHi(){ 7         System.out.println("Hi!"); 8       } 9     }
image from book

Referring to example 10.4 — the its_b attribute is declared and initialized to null on line 2. In the A() constructor on line 4 a new B object is created and its memory location is assigned to the its_b reference. In this manner the A object controls the creation of the B object.

Example 10.6 gives a modified version of the TestDriver program. Compare example 10.6 with example 10.3 above. This version is exactly one line shorter than the previous version. This is because there’s no need to create a B object before creating the A object. Figure 10-7 shows the results of running this program.

Example 10.6: TestDriver.java

image from book
 1     public class TestDriver { 2      public static void main(String[] args){ 3         A a = new A(); 4         a.makeContainedObjectSayHi(); 5       } 6     }
image from book

image from book
Figure 10-7: Results of Running Example 10.6

Quick Review

The UML class diagram can be used to show aggregation associations between classes. A hollow diamond is used to denote simple aggregation and expresses a “uses” or “uses a” relationship between the whole and part classes. A solid diamond is used to show composite aggregation and expresses a “has” or “has a” relationship between whole and part classes.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net