Themes

There are several themes in the GOF design patterns book that are important to note during its study. This section will briefly go over some of these ideas.

Programming to an Interface

The interface of an object is the set of all signatures for all operations on the object. The implementation is the internal data representation of the object.

The GOF book refers to the idea of class vs. type. A class defines the implementation of its object as well as the operations that can be performed. The idea of type simply refers to the interface. That is, the type of an object specifies the operations and requests that can be made to an object.

The GOF book also distinguishes the idea of class inheritance and interface inheritance. Interface inheritance allows objects to share an interface. In other words, what you can do to one object you can do to another because they inherit the same interface. Implementation inheritance refers to sharing of an internal representation and code.

In C++, interface inheritance accomplished using pure abstract base classes. An abstract base class is any base class where there is at least one pure virtual function. A pure abstract base class is an abstract base class where every virtual function is pure. That is a pure abstract base class does not supply any implementation to its derived classes. It simply act as an interface.

The idea of "Programming to an interface not an implementation" refers to writing code using only the operations defined by the interface. The code does not explicitly refer to the concrete objects. Doing this will greatly reduce dependencies.

Composition over Inheritance

Composition and inheritance are both methods that allow for reusing functionality. Inheritance allows you to inherit implementation from the parent class. The internal implementation of the parent class is often visible to the subclass. This is why this type of reuse is sometimes referred to as white-box reuse. Object composition refers to assembling objects to get more functionality. The objects of the composition are used only through their interfaces and the internal implementation is not exposed. This type of reuse is therefore sometimes referred to as black-box reuse.

Using inheritance, the child classes will often inherit internal data representation from the parent. Changes made to the implementation of the parent class, often requires changes to the child class. This makes inheritance less flexible and less reusable. One method of avoiding problems is to inherit only from abstract classes. This way very little if any implementation is inherited.

The idea here is that as systems depend more on object compositition and less on inheritance, there will be a shift from hard coding fixed behaviours to a smaller set of fundamental behaviours that can be composed into more complex ones.

results matching ""

    No results matching ""