Composition instead of inheritance

from Wikipedia, the free encyclopedia
The diagram shows how the flying and speaking behavior can be designed flexibly through composition.

Composition instead of inheritance ( composition over inheritance or composite reuse principle ) is a technique in software design. This principledecouples classes , which leads to more flexible and stable designs. It is thus possible to change the behavior of a class at runtime.

Basics

With this design principle, algorithms that can be used in a class are designed as separate classes, which are preferably derived from interfaces . The classes that should possibly execute one of these algorithms contain a member variable of the type of the common interface that can be assigned to one of these algorithms (also at runtime). This separates the classes from the algorithms and their details - further developments and changes to the algorithm classes have hardly any influence on the class, which makes adjustments unnecessary. This does not change the code, which in turn advocates a different design principle, the open-closed principle .

example

The following Java example is the implementation of the design given above.

/**
 * Interface for everything flyable
 *
 */
public interface Flyable {
    /**
     * Method that implements flying behaviour
     */
    public void fly();
}
/**
 * Interface for everything that gives a quacking sound
 *
 */
public interface Quackable {
    /**
     * Method for the quacking sound
     */
    public void quack();
}
/**
 *
 */
public abstract class Duck implements Ducklike, Comparable<Object> {
    /** Flying behaviour of the duck */
    protected Flyable flyBehavior = null;
    /** Quack behaviour of the duck */
    protected Quackable quackBehavior = null;

    /**
     * Standard constructor
     */
    public Duck() {
        initialize();
    }

    /**
     * Constructor
     * @param WEIGHT Weight of the duck
     */
    public Duck(final float WEIGHT) {
        initialize();
        weight = WEIGHT;
    }

    protected abstract void initialize();

    /**
     * Display method
     */
    public abstract void display();

    /**
     * Performs the flying behaviour
     */
    public void fly() {
        flyBehavior.fly();
    }

    /**
     * Performs the quacking behaviour
     */
    public void quack() {
        quackBehavior.quack();
    }

    /**
     * Setter for the fly behaviour
     * @param FLYING
     */
    public void setFlyBehavior(final Flyable FLYING) {
        flyBehavior = FLYING;
    }

    /**
     * Setter for the quack behaviour
     * @param QUACKING
     */
    public void setQuackBehavior(final Quackable QUACKING) {
        quackBehavior = QUACKING;
    }
}

See also

Individual evidence

  1. a b c Eric Freeman, Elisabeth Freeman, Sierra Kathy, Bates Bert: Mike Hendrickson (Ed.): Mike Loukides (Ed.): Head First Design Patterns ( English , paperback), Volume 1, O'REILLY, 2004, ISBN 978-0-596-00712-6 , p. 23 (Accessed September 24, 2012).