From the course: C++ Design Patterns: Structural

Overview

- [Instructor] Bridge is a structural design pattern that can solve design problems that involve complex class hierarchies also known as the inheritance explosion problem. You will know you have this problem if the number of derived classes keeps growing as the design evolves. The bridge pattern solves the exploding class hierarchy problem by separating common and specific parts of a design into separate hierarchies. Here's an example of an inheritance explosion. In this design, each user interface element can have a different style. The design requires four classes for each element: a concrete implementation of the element and three variations in style. The design is already becoming very complex, and it will only get worse as more elements and styles are added. The bridge design pattern solves this problem by separating the class hierarchy into two hierarchies, one for the element implementations and one for the style variations. Element holds a reference to style, making it possible to compose the design from any combination of element and style. This reference acts as a bridge between the two hierarchies. By favoring composition over inheritance, the design is much simpler, more flexible, and open to future changes. To add a new style, say bold style, we just need to create a new style class and we're good to go. In summary, the bridge design pattern attempts to solve the inheritance explosion problem by switching from inheritance to object composition. In the following videos, we're going to refactor a C++ design using the bridge design pattern.

Contents