Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
The most important contribution of GoF may not be a pattern,
but rather a maxim that they introduce in Chapter 1: Favor object composition
over class inheritance. Understanding inheritance and polymorphism is such a
challenge that you may begin to assign undue importance to these techniques. We
see many over-complicated designs (our own included) that result from
inheritance indulgence for example, many multiple inheritance designs evolve
by insisting that inheritance be used everywhere.
One of the guidelines in Extreme Programming is Do the simplest thing that could possibly work. A design that seems to want
inheritance can often be dramatically simplified by using composition instead,
and you will also discover that the result is more flexible, as you will
understand by studying some of the design patterns in this chapter. So when
pondering a design, ask yourself: Could this be simpler using composition? Do
I really need inheritance here, and what is it buying me?
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |