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

Skip to content

Commit 3907683

Browse files
Try an outline where solution comes first
1 parent a62e6a3 commit 3907683

File tree

1 file changed

+106
-5
lines changed

1 file changed

+106
-5
lines changed

gang-of-four/bridge/index.rst

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
A class that was intended to be simple can, in practice,
2121
wind up getting extended in several different directions
2222
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.
23+
If a system accretes _m_ different variants of a class
24+
along one axis of its design
25+
and _n_ variants along another axis,
26+
then in the worst case a class might need _m×n_ subclasses.
2627

2728
The Gang of Four offer the example of a ``Window`` class
2829
that has been extended to support two different operating systems —
@@ -41,15 +42,115 @@ In their example,
4142
an outer ``Window`` or ``IconWindow`` class
4243
that is purely concerned with layout
4344
can wrap an inner ``MSWindowDriver`` or ``MacWindowDriver`` class
44-
that’s concerned with operating under a specific operating system.
45+
that’s concerned with how to draw pixels under a specific operating system.
4546
The system winds up with _m+n_ instead of _m×n_ separate classes.
4647

4748
As we will see,
4849
this Bridge Pattern not only works well in Python,
4950
but can replace two Python habits
5051
that were not widespread in the Gang of Four’s original languages:
51-
multiple inheritance, and mixins.
52+
multiple inheritance and mixins.
5253

54+
A complicated class
55+
-------------------
56+
57+
.. todo link
58+
59+
Python’s own ``logging`` Standard Library module
60+
makes extensive use of the Bridge Pattern,
61+
so let’s adopt a simple logger as our example.
62+
Imagine that a novice programmer
63+
with no experience extending a logger
64+
started with an overly simple class:
65+
66+
class logger
67+
68+
As the programmer’s team extended the system,
69+
several variants of this basic logger might get written.
70+
One variant might specialize the logger so that it filters log messages,
71+
displaying only messages of at least a given level of severity:
72+
73+
ex
74+
75+
Another variant might allow the destination file to be specialized:
76+
77+
ex
78+
79+
Given these two subclasses,
80+
what happens when the day arrives
81+
when the system needs a logger
82+
that both filters its messages
83+
but also delivers them to a custom destination file?
84+
85+
In the traditional case,
86+
code would need to be duplicated
87+
to produce a class with the abilities of both subclasses.
88+
89+
90+
1 specalizing by subclassing.
91+
if instead working in a monorepo,
92+
and the original class can be extended,
93+
then no problem.
94+
95+
2 >1 axes.
96+
here they are filtering, and emitting.
97+
98+
3 want to avoid duplicating code.
99+
otherwise you can just create a new class
100+
that combines.
101+
102+
in languages used by the gang of four,
103+
point 3 above is much stronger.
104+
you don’t just need to avoid duplicate code,
105+
it’s mandatory that your new logging classes
106+
all inherit from ?
107+
108+
why subclass?
109+
110+
why are they subclassing
111+
instead of extending the original class?
112+
113+
so this is “complicated” because...
114+
115+
The solution
116+
------------
117+
118+
which becomes the wrapper?
119+
120+
* we could decide that a Logger’s primary job is to emit messages,
121+
and so split off filtering into its own ``Filter`` class.
122+
Each logger would be given a ``Filter`` instance
123+
to which it submitted each message before emitting it.
124+
125+
* we could decide that a logger’s primary job is to filter messages.
126+
The task of emitting messages could be ceded to a ``Handler`` class.
127+
Each logger would keep a ``Handler`` instance
128+
to which the logger passed each message that survived filtering.
129+
130+
* The Standard Library logging module
131+
applies the Bridge Pattern in both ways,
132+
splitting out both filters and handlers
133+
as separate classes from the main ``Logger`` class.
134+
135+
to keep our example here simple,
136+
let’s split the emitter.
137+
this is symmetrical with the Gang of Four’s example,
138+
where the wrapper
139+
140+
you can plug using code
141+
142+
dodge: use multiple inheritance
143+
144+
problem is init methods
145+
146+
dodge: use mixins
147+
148+
having an emit method
149+
so that you can specialize it separately
150+
151+
appendix: creating classes dynamically
152+
153+
appendix: functions instead of classes
53154

54155

55156

0 commit comments

Comments
 (0)