2.2. Identify when and how to use composition to solve business problems - Source Code

Composition example:

package c2.s2.composition;

import javax.annotation.Resource;

/**
 * @author Mikalai Zaikin
 */
public class Airplane {
    @Resource
    FlyBehaviour fb;

    public void fly() {
        fb.fly();
    }

    public void startEngine() {
        // ...
    }
}
					

package c2.s2.composition;

import javax.annotation.Resource;

/**
 * @author Mikalai Zaikin
 */
public class Bird {

    @Resource
    FlyBehaviour fb;

    public void fly() {
        fb.fly();
    }

    public void tweet() {
        // ...
    }
}
					

package c2.s2.composition;

/**
 * @author Mikalai Zaikin
 */
public class FlyBehaviour {

    public void fly() {
        // ...
    }
}
					

Inheritance example:

package c2.s2.inheritance;

/**
 * @author Mikalai Zaikin
 */
public class Airplane {
    public void startEngine() {
        // ...
    }

    public void fly() {
        // ...
    }
}
					

package c2.s2.inheritance;

/**
 * @author Mikalai Zaikin
 */
public class Cessna extends Airplane {
    public void setTurboMode(boolean b) {
        // ...
    }
}
					

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Free SCDJWS 5.0 Guide