Aggregation Tutorial

I highly recommend watching my Composition Tutorial prior to watching this one. We learned that an object demonstrating composition is composed of other objects that are created inside of the object during instantiation. The lifetime of those "internal" objects are entirely dependent upon the enclosing object. When the instance of the enclosing object is destroyed, those "internal" objects are destroyed as well. Aggregation is very similar to composition only with one key difference. An object demonstrating aggregation is composed of other objects that are created outside of the object during instantiation. In aggregation, the lifetime of those "internal" objects are not dependent upon the enclosing object. When the instance of the enclosing object is destroyed, those "internal" objects are still alive because they were created elsewhere. Consider the following code that demonstrates aggregation:
class MegaOfficeSuiteDVD {
     void installSoftware( ... );
     void viewShinySurface() { ... }
}
class OperatingSystem {
     void bootUp() { ... }
     void shutDown() { ... }
}
class PowerSupply {
     void turnOn() { ... }
     void turnOff() { ... }
}
class DVDDrive {
     void open() { ... }
     void close() { ... }
}
class Computer{
     private OperatingSystem os = new OperatingSystem();
// composition
     private PowerSupply ps = new PowerSupply();
// composition
     private DVDDrive drive = new DVDDrive();
// composition
     private MegaOfficeSuiteDVD dvd;
// reference only - potential for aggregation
     Computer(MegaOfficeSuiteDVD dvd) {
         this.dvd = dvd;
// aggregation
         ps.turnOn();
         os.bootUp();
     }
     void dvdStuff() {
         //open dvd, place dvd in drive, close dvd
         dvd.installSoftware();
         //open dvd, remove dvd, close dvd
     }
     void allDone() {
         os.shutDown();
         ps.turnOff();
     }
}
class TestMe {
     public static void main(String args[]){
         MegaOfficeSuiteDVD dvd = new MegaOfficeSuiteDVD();
         Computer c = new Computer(dvd);
         // Browing the internet....
         // "Writing some source code...
         c.dvdStuff();
         c.allDone();
         c = null;
// The computer object and all the composition objects are destroyed and eligible for garbage collection
         dvd.viewShinySurface();
// it's alive!
     }
}
In the example above the difference between composition and aggregation becomes apparent when the statement c = null; is invoked. The MegaOfficeSuiteDVD object continues to live after the Computer object and its composition objects are destroyed. At one time the Computer object did consist of a MegaOfficeSuiteDVD, but it was merely just a reference to the actual object created outside of the computer object.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md Aggregation
C:\Java>cd Aggregation
C:\Java\Aggregation>Notepad Aggregation.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Aggregation {
    public static void main(String args[]){
        MegaOfficeSuiteDVD dvd = new MegaOfficeSuiteDVD();
        Computer c = new Computer(dvd);
        System.out.println("\nBrowing the internet...");
        System.out.println("Writing some source code...");
        c.dvdStuff();
        System.out.println("Saving work...\n");
        c.allDone();
        c = null;  // The computer object and all the composition objects are destroyed and eligible for garbage collection
        dvd.viewShinySurface(); // it's alive!
    }
}

class OperatingSystem {
    void bootUp() { System.out.println("OS is booting up"); }
    void shutDown() { System.out.println("OS is shutting down"); }
}

class PowerSupply {
    void turnOn() { System.out.println("Powering on"); }
    void turnOff() { System.out.println("Powering off"); }
}

class DVDDrive {
    void open() { System.out.println("DVD drive is open"); }
    void close() { System.out.println("DVD drive is closed"); }
}

class MegaOfficeSuiteDVD {
    void installSoftware() { System.out.println("Installation of Mega Office Suite complete!"); }
    void viewShinySurface() { System.out.println("\nJust look at that shiny prismy reflection ..."); }
}

class Computer {
    private OperatingSystem os = new OperatingSystem(); // composition
    private PowerSupply ps = new PowerSupply(); // composition
    private DVDDrive drive = new DVDDrive(); // composition
    private MegaOfficeSuiteDVD dvd; // reference only - potential for aggregation

    Computer(MegaOfficeSuiteDVD dvd) {
        this.dvd = dvd; // aggregation
        ps.turnOn();
        os.bootUp();
    }

    void dvdStuff() {
        drive.open();
        System.out.println("Placing DVD in tray...");
        drive.close();
        dvd.installSoftware();
        drive.open();
        System.out.println("Removing DVD from tray...");
        drive.close();
    }    
    void allDone() {
        os.shutDown();
        ps.turnOff();
    }
}


Now switch back to the command prompt (CMD) and type in javac Aggregation.java and press Enter.
Now type in java Aggregation and press Enter.


C:\Java\Aggregation>javac Aggregation.java
C:\Java\Aggregation>java Aggregation
See video for results


Final thoughts

None


Tutorials