Favor composition over inheritance

Also known as

Composite Reuse Principle

Motivation

When some behaviour of a domain object can change with other features remaining the same.

Applicability

Whenever class hierarchies are being created to specialize behaviour then behaviour could be extracted in an interface. The primary object would delegate the behaviour to interface implementations.

This approach avoids the number of classes to be created. Same set of classes composed differently give different run-time behaviour.

Application

Design classes to use interfaces for variable behaviour and delegate the behaviour to a concrete class.

Consequences

Lesser number of classes and multiple behaviour with composition.

Extensibility

Samples

If you have different devices that use the same software and only differ in the display hardware. Then you could use two approaches.

Template Method

public class Device {
 public void display() {
    // Basic display
 }
}

public class LEDDisplayDevice {
 public void display() {
 }
}

public class ExternalDisplayDevice {
 public void display() {
 }
}

Using Composition (Strategy Pattern)

public class Device {
 private Display display;

 public Device(Display display) {
   this.display = display;
 }

 public void display() {
    this.display.display();
 }
}

public class LEDDisplay {
 public void display() {
 }
}

public class ExternalDisplay {
 public void display() {
 }
}

References

Composite Reuse Principle

The Xebia Essentials Cards

This page is part of the Xebia Essentials, a pack of flash cards about Software Development Done Right. You can get your own deck of Essentials cards in the Xebia store.