One object-oriented design principle put forth in the original design pattern book by Gamma et al is to favor object composition over class inheritance. That is, when possible, design your programs using object composition instead of beginning with a superclass and then accomplishing reuse through inheritance. To understand the reason for this dictum, we need to look at object composition.
To appreciate the advantage of object composition over inheritance, we need to understand another principle put forth by Gamma, Helm, Johnson and Vlissides:
Program to an interface, not an implementation.
Let's first look at why programming to an interface is favored over programming to an implementation.
In a nutshell, according to Gamma et al, programming to an interface is better than programming to an implementation because it reduces implementation dependencies between subsystems. This increases software flexibility. As a result, changes can be made at runtime.
The idea of programming to an interface does not literally mean an
ActionScript 3.0 construct
opposed to a
class. That is, you can program
to the interface of an abstract class or an interface instead of a class. What
does that really mean, though?
The easiest way to understand this concept is to think of the methods in either an abstract class or interface as interfaces. An interface is a set of functions an object implements. Eric and Elisabeth Freeman ( ...