Stairway to Heaven

Intent This pattern describes the network of inheritance relationships that is needed when a given hierarchy must be adapted, in its entirety, to another class.

Motivation If a class hierarchy is to be reusable, it cannot depend upon detailed implementations. For example, one might have a class hierarchy of payroll objects such as: Employee, SalariedEmployee, HourlyEmployee, etc. If these classes focus only upon the algorithms necessary to implement their particular abstractions, then they are highly reusable. However, if they were to incorporate the methods for reading and writing such objects on a particular database engine, then they would not be reusable in applications that did not have access to, or need that particular engine. Thus, we would like to keep any knowledge of database engines out of these objects.

Solution To do this, a new set of classes needs to be created that inherits the ability to read and write themselves using the particular database engine required by the application, and the methods that model the payroll abstraction. This keeps the payroll objects separate and, reusable.

Notes The structure diagram shows the use of virtual inheritance within the structure of this pattern. This is a C++-ism, but has correspondents in other languages. The use of virtual inheritance is necessary in this pattern to prevent the repeated inheritance of the base classes. The desire is that there be only one copy of all the base objects in any of the derived objects.

Applicability This pattern is best used to insure the isolation of concepts so that reusable class hierarchies do not become polluted with concepts that are application specific.

[Source: Robert Martin, "Oh! What a relief it is!", C++ Report, Feb 96, p76]