Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a62e6a3

Browse files
First draft of a real introduction
1 parent 8d9eb99 commit a62e6a3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

gang-of-four/bridge/index.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,44 @@
1717
that simpler classes be composed together
1818
to perform the same task.
1919

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+
2058
object oriented folks will have seen situations
2159
where a single class needs to be specialized
2260
along many different axes at once

0 commit comments

Comments
 (0)