|
17 | 17 | that simpler classes be composed together
|
18 | 18 | to perform the same task.
|
19 | 19 |
|
| 20 | +A class that was intended to be simple can, in practice, |
| 21 | +wind up getting extended in several different directions |
| 22 | +over the course of its career. |
| 23 | +If the class varies in _m_ different ways along one axis of design |
| 24 | +and _n_ ways along another, |
| 25 | +then in the worst case a system might need _m×n_ variants of the class. |
| 26 | + |
| 27 | +The Gang of Four offer the example of a ``Window`` class |
| 28 | +that has been extended to support two different operating systems — |
| 29 | +imagine that there are subclasses ``MSWindow`` and ``MacWindow``. |
| 30 | +What happens when a designer now wants to create a special window |
| 31 | +for supporting a grid of icons? |
| 32 | +They will need to create both an ``MSIconWindow`` and a ``MacIconWindow``. |
| 33 | +The _m_=2 possible operating systems (Microsoft and Windows) |
| 34 | +have combined with _n_=2 possible behaviors (“plain” and “icon”) |
| 35 | +to produce 2×2=4 classes. |
| 36 | + |
| 37 | +The Bridge Pattern recommends splitting a complicated class |
| 38 | +into several simple classes, |
| 39 | +each of which implements one of the possible axes of design. |
| 40 | +In their example, |
| 41 | +an outer ``Window`` or ``IconWindow`` class |
| 42 | +that is purely concerned with layout |
| 43 | +can wrap an inner ``MSWindowDriver`` or ``MacWindowDriver`` class |
| 44 | +that’s concerned with operating under a specific operating system. |
| 45 | +The system winds up with _m+n_ instead of _m×n_ separate classes. |
| 46 | + |
| 47 | +As we will see, |
| 48 | +this Bridge Pattern not only works well in Python, |
| 49 | +but can replace two Python habits |
| 50 | +that were not widespread in the Gang of Four’s original languages: |
| 51 | +multiple inheritance, and mixins. |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
20 | 58 | object oriented folks will have seen situations
|
21 | 59 | where a single class needs to be specialized
|
22 | 60 | along many different axes at once
|
|
0 commit comments