1
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
3
<title>Twisted Documentation: Components: Interfaces and Adapters</title>
4
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
8
<h1 class="title">Components: Interfaces and Adapters</h1>
9
<div class="toc"><ol><li><a href="#auto0">Interfaces and Components in Twisted code</a></li><ul><li><a href="#auto1">Components and Inheritance</a></li></ul></ol></div>
13
<p>Object oriented programming languages allow programmers to reuse portions of
14
existing code by creating new <q>classes</q> of objects which subclass another
15
class. When a class subclasses another, it is said to <em>inherit</em> all of its
16
behaviour. The subclass can then <q>override</q> and <q>extend</q> the behavior
17
provided to it by the superclass. Inheritance is very useful in many situations,
18
but because it is so convenient to use, often becomes abused in large software
19
systems, especially when multiple inheritance is involved. One solution is to
20
use <em>delegation</em> instead of <q>inheritance</q> where appropriate.
21
Delegation is simply the act of asking <em>another</em> object to perform a task
22
for an object. To support this design pattern, which is often referred to as the
23
<em>components</em> pattern because it involves many small interacting components,
24
<em>interfaces</em> and <em>adapters</em> were created by the Zope 3 team.</p>
26
<p><q>Interfaces</q> are simply markers which objects can use to say <q>I
27
implement this interface</q>. Other objects may then make requests like
28
<q>Please give me an object which implements interface X for object type Y</q>.
29
Objects which implement an interface for another object type are called
32
<p>The superclass-subclass relationship is said to be an <em>is-a</em> relationship.
33
When designing object hierarchies, object modellers use subclassing when they
34
can say that the subclass <em>is</em> the same class as the superclass. For
37
<pre class="python"><p class="py-linenumber"> 1
55
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">Shape</span>:
56
<span class="py-src-variable">sideLength</span> = <span class="py-src-number">0</span>
57
<span class="py-src-keyword">def</span> <span class="py-src-identifier">getSideLength</span>(<span class="py-src-parameter">self</span>):
58
<span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">sideLength</span>
60
<span class="py-src-keyword">def</span> <span class="py-src-identifier">setSideLength</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">sideLength</span>):
61
<span class="py-src-variable">self</span>.<span class="py-src-variable">sideLength</span> = <span class="py-src-variable">sideLength</span>
63
<span class="py-src-keyword">def</span> <span class="py-src-identifier">area</span>(<span class="py-src-parameter">self</span>):
64
<span class="py-src-keyword">raise</span> <span class="py-src-variable">NotImplementedError</span>, <span class="py-src-string">"Subclasses must implement area"</span>
66
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Triangle</span>(<span class="py-src-parameter">Shape</span>):
67
<span class="py-src-keyword">def</span> <span class="py-src-identifier">area</span>(<span class="py-src-parameter">self</span>):
68
<span class="py-src-keyword">return</span> (<span class="py-src-variable">self</span>.<span class="py-src-variable">sideLength</span> * <span class="py-src-variable">self</span>.<span class="py-src-variable">sideLength</span>) / <span class="py-src-number">2</span>
70
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Square</span>(<span class="py-src-parameter">Shape</span>):
71
<span class="py-src-keyword">def</span> <span class="py-src-identifier">area</span>(<span class="py-src-parameter">self</span>):
72
<span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">sideLength</span> * <span class="py-src-variable">self</span>.<span class="py-src-variable">sideLength</span>
75
<p>In the above example, a Triangle <em>is-a</em> Shape, so it subclasses Shape,
76
and a Square <em>is-a</em> Shape, so it also subclasses Shape.</p>
78
<p>However, subclassing can get complicated, especially when Multiple
79
Inheritance enters the picture. Multiple Inheritance allows a class to inherit
80
from more than one base class. Software which relies heavily on inheritance
81
often ends up having both very wide and very deep inheritance trees, meaning
82
that one class inherits from many superclasses spread throughout the system.
83
Since subclassing with Multiple Inheritance means <em>implementation
84
inheritance</em>, locating a method's actual implementation and ensuring the
85
correct method is actually being invoked becomes a challenge. For example:</p>
87
<pre class="python"><p class="py-linenumber"> 1
109
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">Area</span>:
110
<span class="py-src-variable">sideLength</span> = <span class="py-src-number">0</span>
111
<span class="py-src-keyword">def</span> <span class="py-src-identifier">getSideLength</span>(<span class="py-src-parameter">self</span>):
112
<span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">sideLength</span>
114
<span class="py-src-keyword">def</span> <span class="py-src-identifier">setSideLength</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">sideLength</span>):
115
<span class="py-src-variable">self</span>.<span class="py-src-variable">sideLength</span> = <span class="py-src-variable">sideLength</span>
117
<span class="py-src-keyword">def</span> <span class="py-src-identifier">area</span>(<span class="py-src-parameter">self</span>):
118
<span class="py-src-keyword">raise</span> <span class="py-src-variable">NotImplementedError</span>, <span class="py-src-string">"Subclasses must implement area"</span>
120
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Color</span>:
121
<span class="py-src-variable">color</span> = <span class="py-src-variable">None</span>
122
<span class="py-src-keyword">def</span> <span class="py-src-identifier">setColor</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">color</span>):
123
<span class="py-src-variable">self</span>.<span class="py-src-variable">color</span> = <span class="py-src-variable">color</span>
125
<span class="py-src-keyword">def</span> <span class="py-src-identifier">getColor</span>(<span class="py-src-parameter">self</span>):
126
<span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">color</span>
128
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Square</span>(<span class="py-src-parameter">Area</span>, <span class="py-src-parameter">Color</span>):
129
<span class="py-src-keyword">def</span> <span class="py-src-identifier">area</span>(<span class="py-src-parameter">self</span>):
130
<span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">sideLength</span> * <span class="py-src-variable">self</span>.<span class="py-src-variable">sideLength</span>
133
<p>The reason programmers like using implementation inheritance is because it
134
makes code easier to read since the implementation details of Area are in a
135
separate place than the implementation details of Color. This is nice, because
136
conceivably an object could have a color but not an area, or an area but not a
137
color. The problem, though, is that Square is not really an Area or a Color, but
138
has an area and color. Thus, we should really be using another object oriented
139
technique called <em>composition</em>, which relies on delegation rather than
140
inheritance to break code into small reusable chunks. Let us continue with the
141
Multiple Inheritance example, though, because it is often used in practice.</p>
143
<p>What if both the Color and the Area base class defined the same method,
144
perhaps <code>calculate</code>? Where would the implementation come from? The
145
implementation that is located for <code>Square().calculate()</code> depends on
146
the method resolution order, or MRO, and can change when programmers change
147
seemingly unrelated things by refactoring classes in other parts of the system,
148
causing obscure bugs. Our first thought might be to change the calculate method
149
name to avoid name clashes, to perhaps <code>calculateArea</code> and
150
<code>calculateColor</code>. While explicit, this change could potentially
151
require a large number of changes throughout a system, and is error-prone,
152
especially when attempting to integrate two systems which you didn't write.</p>
154
<p>Let's imagine another example. We have an electric appliance, say a hair
155
dryer. The hair dryer is american voltage. We have two electric sockets, one of
156
them an american 110 Volt socket, and one of them a foreign 220 Volt socket. If
157
we plug the hair dryer into the 220 Volt socket, it is going to expect 110 Volt
158
current and errors will result. Going back and changing the hair dryer to
159
support both <code>plug110Volt</code> and <code>plug220Volt</code> methods would
160
be tedious, and what if we decided we needed to plug the hair dryer into yet
161
another type of socket? For example:</p>
163
<pre class="python"><p class="py-linenumber"> 1
178
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">HairDryer</span>:
179
<span class="py-src-keyword">def</span> <span class="py-src-identifier">plug</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">socket</span>):
180
<span class="py-src-keyword">if</span> <span class="py-src-variable">socket</span>.<span class="py-src-variable">voltage</span>() == <span class="py-src-number">110</span>:
181
<span class="py-src-keyword">print</span> <span class="py-src-string">"I was plugged in properly and am operating."</span>
182
<span class="py-src-keyword">else</span>:
183
<span class="py-src-keyword">print</span> <span class="py-src-string">"I was plugged in improperly and "</span>
184
<span class="py-src-keyword">print</span> <span class="py-src-string">"now you have no hair dryer any more."</span>
186
<span class="py-src-keyword">class</span> <span class="py-src-identifier">AmericanSocket</span>:
187
<span class="py-src-keyword">def</span> <span class="py-src-identifier">voltage</span>(<span class="py-src-parameter">self</span>):
188
<span class="py-src-keyword">return</span> <span class="py-src-number">110</span>
190
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ForeignSocket</span>:
191
<span class="py-src-keyword">def</span> <span class="py-src-identifier">voltage</span>(<span class="py-src-parameter">self</span>):
192
<span class="py-src-keyword">return</span> <span class="py-src-number">220</span>
195
<p>Given these classes, the following operations can be performed:</p>
197
<pre class="python-interpreter" xml:space="preserve">
198
>>> hd = HairDryer()
199
>>> am = AmericanSocket()
200
>>> hd.plug(am)
201
I was plugged in properly and am operating.
202
>>> fs = ForeignSocket()
203
>>> hd.plug(fs)
204
I was plugged in improperly and
205
now you have no hair dryer any more.
208
<p>We are going to attempt to solve this problem by writing an Adapter for the
209
<code>ForeignSocket</code> which converts the voltage for use with an American
210
hair dryer. An Adapter is a class which is constructed with one and only one
211
argument, the <q>adaptee</q> or <q>original</q> object. In this example, we
212
will show all code involved for clarity:</p>
214
<pre class="python"><p class="py-linenumber">1
220
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">AdaptToAmericanSocket</span>:
221
<span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">original</span>):
222
<span class="py-src-variable">self</span>.<span class="py-src-variable">original</span> = <span class="py-src-variable">original</span>
224
<span class="py-src-keyword">def</span> <span class="py-src-identifier">voltage</span>(<span class="py-src-parameter">self</span>):
225
<span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">original</span>.<span class="py-src-variable">voltage</span>() / <span class="py-src-number">2</span>
228
<p>Now, we can use it as so:</p>
230
<pre class="python-interpreter" xml:space="preserve">
231
>>> hd = HairDryer()
232
>>> fs = ForeignSocket()
233
>>> adapted = AdaptToAmericanSocket(fs)
234
>>> hd.plug(adapted)
235
I was plugged in properly and am operating.
238
<p>So, as you can see, an adapter can 'override' the original implementation. It
239
can also 'extend' the interface of the original object by providing methods the
240
original object did not have. Note that an Adapter must explicitly delegate any
241
method calls it does not wish to modify to the original, otherwise the Adapter
242
cannot be used in places where the original is expected. Usually this is not a
243
problem, as an Adapter is created to conform an object to a particular interface
244
and then discarded.</p>
246
<h2>Interfaces and Components in Twisted code<a name="auto0"/></h2>
248
<p>Adapters are a useful way of using multiple classes to factor code into
249
discrete chunks. However, they are not very interesting without some more
250
infrastructure. If each piece of code which wished to use an adapted object had
251
to explicitly construct the adapter itself, the coupling between components
252
would be too tight. We would like to achieve <q>loose coupling</q>, and this is
253
where <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.components.html" title="twisted.python.components">twisted.python.components</a></code> comes in.</p>
255
<p>First, we need to discuss Interfaces in more detail. As we mentioned
256
earlier, an Interface is nothing more than a class which is used as a marker.
257
Interfaces should be subclasses of <code>zope.interface.Interface</code>, and
258
have a very odd look to python programmers not used to them:</p>
260
<pre class="python"><p class="py-linenumber">1
266
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">zope</span>.<span class="py-src-variable">interface</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Interface</span>
268
<span class="py-src-keyword">class</span> <span class="py-src-identifier">IAmericanSocket</span>(<span class="py-src-parameter">Interface</span>):
269
<span class="py-src-keyword">def</span> <span class="py-src-identifier">voltage</span>():
270
<span class="py-src-string">"""Return the voltage produced by this socket object, as an integer.
271
"""</span>
274
<p>Notice how it looks just like a regular class definition, other than
275
inheriting from <code>Interface</code>? However, the method definitions inside
276
the class block do not have any method body! Since Python does not have any
277
native language-level support for Interfaces like Java does, this is what
278
distinguishes an Interface definition from a Class.</p>
280
<p>Now that we have a defined Interface, we can talk about objects using terms
281
like this: <q>The <code>AmericanSocket</code> class implements the
282
<code>IAmericanSocket</code> interface</q> and <q>Please give me an object which
283
adapts <code>ForeignSocket</code> to the <code>IAmericanSocket</code>
284
interface</q>. We can make <em>declarations</em> about what interfaces a certain
285
class implements, and we can request adapters which implement a certain
286
interface for a specific class.</p>
288
<p>Let's look at how we declare that a class implements an interface:</p>
290
<pre class="python"><p class="py-linenumber">1
298
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">zope</span>.<span class="py-src-variable">interface</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">implements</span>
300
<span class="py-src-keyword">class</span> <span class="py-src-identifier">AmericanSocket</span>:
302
<span class="py-src-variable">implements</span>(<span class="py-src-variable">IAmericanSocket</span>)
304
<span class="py-src-keyword">def</span> <span class="py-src-identifier">voltage</span>(<span class="py-src-parameter">self</span>):
305
<span class="py-src-keyword">return</span> <span class="py-src-number">110</span>
308
<p>So, to declare that a class implements an interface, we simply call
309
<code>zope.interface.implements</code> at the class level.</p>
311
<p>Now, let's say we want to rewrite the <code>AdaptToAmericanSocket</code>
312
class as a real adapter. In this case we also specify it as implementing
313
<code>IAmericanSocket</code>:</p>
315
<pre class="python"><p class="py-linenumber"> 1
329
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">zope</span>.<span class="py-src-variable">interface</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">implements</span>
331
<span class="py-src-keyword">class</span> <span class="py-src-identifier">AdaptToAmericanSocket</span>:
333
<span class="py-src-variable">implements</span>(<span class="py-src-variable">IAmericanSocket</span>)
335
<span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">original</span>):
336
<span class="py-src-string">"""
337
Pass the original ForeignSocket object as original
338
"""</span>
339
<span class="py-src-variable">self</span>.<span class="py-src-variable">original</span> = <span class="py-src-variable">original</span>
341
<span class="py-src-keyword">def</span> <span class="py-src-identifier">voltage</span>(<span class="py-src-parameter">self</span>):
342
<span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">original</span>.<span class="py-src-variable">voltage</span>() / <span class="py-src-number">2</span>
345
<p>Notice how we placed the implements declaration on this adapter class. So
346
far, we have not achieved anything by using components other than requiring us
347
to type more. In order for components to be useful, we must use the
348
<em>component registry</em>. Since <code>AdaptToAmericanSocket</code> implements
349
<code>IAmericanSocket</code> and regulates the voltage of a
350
<code>ForeignSocket</code> object, we can <em>register
351
<code>AdaptToAmericanSocket</code> as an <code>IAmericanSocket</code> adapter
352
for the <code>ForeignSocket</code> class</em>. It is easier to see how this is
353
done in code than to describe it:</p>
355
<pre class="python"><p class="py-linenumber"> 1
387
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">zope</span>.<span class="py-src-variable">interface</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Interface</span>, <span class="py-src-variable">implements</span>
388
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">components</span>
390
<span class="py-src-keyword">class</span> <span class="py-src-identifier">IAmericanSocket</span>(<span class="py-src-parameter">Interface</span>):
391
<span class="py-src-keyword">def</span> <span class="py-src-identifier">voltage</span>():
392
<span class="py-src-string">"""Return the voltage produced by this socket object, as an integer.
393
"""</span>
395
<span class="py-src-keyword">class</span> <span class="py-src-identifier">AmericanSocket</span>:
396
<span class="py-src-variable">implements</span>(<span class="py-src-variable">IAmericanSocket</span>)
398
<span class="py-src-keyword">def</span> <span class="py-src-identifier">voltage</span>(<span class="py-src-parameter">self</span>):
399
<span class="py-src-keyword">return</span> <span class="py-src-number">110</span>
401
<span class="py-src-keyword">class</span> <span class="py-src-identifier">ForeignSocket</span>:
402
<span class="py-src-keyword">def</span> <span class="py-src-identifier">voltage</span>(<span class="py-src-parameter">self</span>):
403
<span class="py-src-keyword">return</span> <span class="py-src-number">220</span>
405
<span class="py-src-keyword">class</span> <span class="py-src-identifier">AdaptToAmericanSocket</span>:
407
<span class="py-src-variable">implements</span>(<span class="py-src-variable">IAmericanSocket</span>)
409
<span class="py-src-keyword">def</span> <span class="py-src-identifier">__init__</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">original</span>):
410
<span class="py-src-variable">self</span>.<span class="py-src-variable">original</span> = <span class="py-src-variable">original</span>
412
<span class="py-src-keyword">def</span> <span class="py-src-identifier">voltage</span>(<span class="py-src-parameter">self</span>):
413
<span class="py-src-keyword">return</span> <span class="py-src-variable">self</span>.<span class="py-src-variable">original</span>.<span class="py-src-variable">voltage</span>() / <span class="py-src-number">2</span>
415
<span class="py-src-variable">components</span>.<span class="py-src-variable">registerAdapter</span>(
416
<span class="py-src-variable">AdaptToAmericanSocket</span>,
417
<span class="py-src-variable">ForeignSocket</span>,
418
<span class="py-src-variable">IAmericanSocket</span>)
421
<p>Now, if we run this script in the interactive interpreter, we can discover a
422
little more about how to use components. The first thing we can do is discover
423
whether an object implements an interface or not:</p>
425
<pre class="python-interpreter" xml:space="preserve">
426
>>> IAmericanSocket.implementedBy(AmericanSocket)
428
>>> IAmericanSocket.implementedBy(ForeignSocket)
430
>>> am = AmericanSocket()
431
>>> fs = ForeignSocket()
432
>>> IAmericanSocket.providedBy(am)
434
>>> IAmericanSocket.providedBy(fs)
438
<p>As you can see, the <code>AmericanSocket</code> instance claims to
439
implement <code>IAmericanSocket</code>, but the <code>ForeignSocket</code>
440
does not. If we wanted to use the <code>HairDryer</code> with the
441
<code>AmericanSocket</code>, we could know that it would be safe to do so by
442
checking whether it implements <code>IAmericanSocket</code>. However, if we
443
decide we want to use <code>HairDryer</code> with a <code>ForeignSocket</code>
444
instance, we must <em>adapt</em> it to <code>IAmericanSocket</code> before
445
doing so. We use the interface object to do this:</p>
447
<pre class="python-interpreter" xml:space="preserve">
448
>>> IAmericanSocket(fs)
449
<__main__.AdaptToAmericanSocket instance at 0x1a5120>
452
<p>When calling an interface with an object as an argument, the interface
453
looks in the adapter registry for an adapter which implements the interface for
454
the given instance's class. If it finds one, it constructs an instance of the
455
Adapter class, passing the constructor the original instance, and returns it.
456
Now the <code>HairDryer</code> can safely be used with the adapted
457
<code>ForeignSocket</code>. But what happens if we attempt to adapt an object
458
which already implements <code>IAmericanSocket</code>? We simply get back the
459
original instance:</p>
461
<pre class="python-interpreter" xml:space="preserve">
462
>>> IAmericanSocket(am)
463
<__main__.AmericanSocket instance at 0x36bff0>
466
<p>So, we could write a new <q>smart</q> <code>HairDryer</code> which
467
automatically looked up an adapter for the socket you tried to plug it into:</p>
469
<pre class="python"><p class="py-linenumber">1
474
</p><span class="py-src-keyword">class</span> <span class="py-src-identifier">HairDryer</span>:
475
<span class="py-src-keyword">def</span> <span class="py-src-identifier">plug</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">socket</span>):
476
<span class="py-src-variable">adapted</span> = <span class="py-src-variable">IAmericanSocket</span>(<span class="py-src-variable">socket</span>)
477
<span class="py-src-keyword">assert</span> <span class="py-src-variable">adapted</span>.<span class="py-src-variable">voltage</span>() == <span class="py-src-number">110</span>, <span class="py-src-string">"BOOM"</span>
478
<span class="py-src-keyword">print</span> <span class="py-src-string">"I was plugged in properly and am operating"</span>
481
<p>Now, if we create an instance of our new <q>smart</q> <code>HairDryer</code>
482
and attempt to plug it in to various sockets, the <code>HairDryer</code> will
483
adapt itself automatically depending on the type of socket it is plugged in
486
<pre class="python-interpreter" xml:space="preserve">
487
>>> am = AmericanSocket()
488
>>> fs = ForeignSocket()
489
>>> hd = HairDryer()
490
>>> hd.plug(am)
491
I was plugged in properly and am operating
492
>>> hd.plug(fs)
493
I was plugged in properly and am operating
496
<p>Voila; the magic of components.</p>
498
<h3>Components and Inheritance<a name="auto1"/></h3>
500
<p>If you inherit from a class which implements some interface, and your new
501
subclass declares that it implements another interface, the implements will be
502
inherited by default.</p>
504
<p>For example, <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.spread.pb.Root.html" title="twisted.spread.pb.Root">pb.Root</a></code> is a class
505
which implements <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.spread.pb.IPBRoot.html" title="twisted.spread.pb.IPBRoot">IPBRoot</a></code>. This interface indicates that an
506
object has remotely-invokable methods and can be used as the initial object
507
served by a new Broker instance. It has an <code>implements</code> setting
510
<pre class="python"><p class="py-linenumber">1
514
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">zope</span>.<span class="py-src-variable">interface</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">implements</span>
516
<span class="py-src-keyword">class</span> <span class="py-src-identifier">Root</span>(<span class="py-src-parameter">Referenceable</span>):
517
<span class="py-src-variable">implements</span>(<span class="py-src-variable">IPBRoot</span>)
520
<p>Suppose you have your own class which implements your
521
<code>IMyInterface</code> interface:</p>
523
<pre class="python"><p class="py-linenumber">1
530
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">zope</span>.<span class="py-src-variable">interface</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">implements</span>, <span class="py-src-variable">Interface</span>
532
<span class="py-src-keyword">class</span> <span class="py-src-identifier">IMyInterface</span>(<span class="py-src-parameter">Interface</span>):
533
<span class="py-src-keyword">pass</span>
535
<span class="py-src-keyword">class</span> <span class="py-src-identifier">MyThing</span>:
536
<span class="py-src-variable">implements</span>(<span class="py-src-variable">IMyInterface</span>)
539
<p>Now if you want to make this class inherit from <code>pb.Root</code>,
540
the interfaces code will automatically determine that it also implements
541
<code>IPBRoot</code>:</p>
543
<pre class="python"><p class="py-linenumber">1
551
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">spread</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">pb</span>
552
<span class="py-src-keyword">from</span> <span class="py-src-variable">zope</span>.<span class="py-src-variable">interface</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">implements</span>, <span class="py-src-variable">Interface</span>
554
<span class="py-src-keyword">class</span> <span class="py-src-identifier">IMyInterface</span>(<span class="py-src-parameter">Interface</span>):
555
<span class="py-src-keyword">pass</span>
557
<span class="py-src-keyword">class</span> <span class="py-src-identifier">MyThing</span>(<span class="py-src-parameter">pb</span>.<span class="py-src-parameter">Root</span>):
558
<span class="py-src-variable">implements</span>(<span class="py-src-variable">IMyInterface</span>)
561
<pre class="python-interpreter" xml:space="preserve">
562
>>> from twisted.spread.flavors import IPBRoot
563
>>> IPBRoot.implementedBy(MyThing)
567
<p>If you want <code>MyThing</code> to inherit from <code>pb.Root</code> but
568
<em>not</em> implement <code>IPBRoot</code> like <code>pb.Root</code> does,
569
use <code>implementOnly</code>:</p>
571
<pre class="python"><p class="py-linenumber">1
579
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">spread</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">pb</span>
580
<span class="py-src-keyword">from</span> <span class="py-src-variable">zope</span>.<span class="py-src-variable">interface</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">implementsOnly</span>, <span class="py-src-variable">Interface</span>
582
<span class="py-src-keyword">class</span> <span class="py-src-identifier">IMyInterface</span>(<span class="py-src-parameter">Interface</span>):
583
<span class="py-src-keyword">pass</span>
585
<span class="py-src-keyword">class</span> <span class="py-src-identifier">MyThing</span>(<span class="py-src-parameter">pb</span>.<span class="py-src-parameter">Root</span>):
586
<span class="py-src-variable">implementsOnly</span>(<span class="py-src-variable">IMyInterface</span>)
589
<pre class="python-interpreter" xml:space="preserve">
590
>>> from twisted.spread.pb import IPBRoot
591
>>> IPBRoot.implementedBy(MyThing)
597
<p><a href="index.html">Index</a></p>
598
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'