| 1 |
============== |
| 2 |
lazr.delegates |
| 3 |
============== |
| 4 |
|
| 5 |
The ``lazr.delegates`` package makes it easy to write objects that delegate |
| 6 |
behavior to another object. The new object adds some property or behavior on |
| 7 |
to the other object, while still providing the underlying interface, and |
| 8 |
delegating behavior. |
| 9 |
|
| 10 |
|
| 11 |
Usage |
| 12 |
===== |
| 13 |
|
| 14 |
The ``@delegate_to`` class decorator makes a class implement zero or more |
| 15 |
interfaces by delegating the implementation to another object. In the case of |
| 16 |
a class providing an adapter, that object will be the *context*, but it can |
| 17 |
really be any object stored in an attribute. So while the interfaces use an |
| 18 |
inheritance mechanism, the classes use a composition mechanism. |
| 19 |
|
| 20 |
For example, we can define two interfaces ``IFoo0`` and ``IFoo1`` where the |
| 21 |
latter inherits from the former. The first interface defines an attribute. |
| 22 |
|
| 23 |
>>> from zope.interface import Interface, Attribute |
| 24 |
>>> class IFoo0(Interface): |
| 25 |
... one = Attribute('attribute in IFoo0') |
| 26 |
|
| 27 |
The second (i.e. derived) interface defines a method and an attribute. |
| 28 |
|
| 29 |
>>> class IFoo1(IFoo0): |
| 30 |
... def bar(): |
| 31 |
... """A method in IFoo1""" |
| 32 |
... baz = Attribute('attribute in IFoo1') |
| 33 |
|
| 34 |
We also define two classes that mirror the interfaces, and do something |
| 35 |
interesting. |
| 36 |
:: |
| 37 |
|
| 38 |
>>> class Foo0: |
| 39 |
... one = 'one' |
| 40 |
|
| 41 |
>>> class Foo1(Foo0): |
| 42 |
... def bar(self): |
| 43 |
... return 'bar' |
| 44 |
... baz = 'I am baz' |
| 45 |
|
| 46 |
Finally, to tie everything together, we can define a class that delegates the |
| 47 |
implementation of ``IFoo1`` to an attribute on the instance. By default, |
| 48 |
``self.context`` is used as the delegate attribute. |
| 49 |
|
| 50 |
>>> from lazr.delegates import delegate_to |
| 51 |
>>> @delegate_to(IFoo1) |
| 52 |
... class SomeClass: |
| 53 |
... def __init__(self, context): |
| 54 |
... self.context = context |
| 55 |
|
| 56 |
When the class doing the delegation is instantiated, an instance of the class |
| 57 |
implementing the interface is passed in. |
| 58 |
|
| 59 |
>>> delegate = Foo1() |
| 60 |
>>> s = SomeClass(delegate) |
| 61 |
|
| 62 |
Now, the ``bar()`` method comes from ``Foo1``. |
| 63 |
|
| 64 |
>>> print(s.bar()) |
| 65 |
bar |
| 66 |
|
| 67 |
The ``baz`` attribute also comes from ``Foo1``. |
| 68 |
|
| 69 |
>>> print(s.baz) |
| 70 |
I am baz |
| 71 |
|
| 72 |
The ``one`` attribute comes from ``Foo0``. |
| 73 |
|
| 74 |
>>> print(s.one) |
| 75 |
one |
| 76 |
|
| 77 |
Even though the interface of ``SomeClass`` is defined through the delegate, |
| 78 |
the interface is still provided by the instance. |
| 79 |
|
| 80 |
>>> IFoo1.providedBy(s) |
| 81 |
True |
| 82 |
|
| 83 |
|
| 84 |
Custom context |
| 85 |
-------------- |
| 86 |
|
| 87 |
The ``@delegate_to`` decorator takes an optional keyword argument to customize |
| 88 |
the attribute containing the object to delegate to. |
| 89 |
|
| 90 |
>>> @delegate_to(IFoo1, context='myfoo') |
| 91 |
... class SomeOtherClass: |
| 92 |
... def __init__(self, foo): |
| 93 |
... self.myfoo = foo |
| 94 |
|
| 95 |
The attributes and methods are still delegated correctly. |
| 96 |
|
| 97 |
>>> s = SomeOtherClass(delegate) |
| 98 |
>>> print(s.bar()) |
| 99 |
bar |
| 100 |
>>> print(s.baz) |
| 101 |
I am baz |
| 102 |
|
| 103 |
|
| 104 |
Multiple interfaces |
| 105 |
=================== |
| 106 |
|
| 107 |
The ``@delegate_to`` decorator accepts more than one interface. Note however, |
| 108 |
that the context attribute must implement all of the named interfaces. |
| 109 |
|
| 110 |
>>> class IFoo2(Interface): |
| 111 |
... another = Attribute('another attribute') |
| 112 |
|
| 113 |
Here is a class that implements the interface. It inherits from the |
| 114 |
implementation class that provides the ``IFoo0`` interface. Thus does this |
| 115 |
class implement both interfaces. |
| 116 |
|
| 117 |
>>> class Foo2(Foo0): |
| 118 |
... another = 'I am another foo' |
| 119 |
|
| 120 |
Again, we tie it all together. |
| 121 |
|
| 122 |
>>> @delegate_to(IFoo0, IFoo2) |
| 123 |
... class SomeOtherClass: |
| 124 |
... def __init__(self, context): |
| 125 |
... self.context = context |
| 126 |
|
| 127 |
Now, the instance of this class has all the expected attributes, and provides |
| 128 |
the expected interfaces. |
| 129 |
|
| 130 |
>>> s = SomeOtherClass(Foo2()) |
| 131 |
>>> print(s.another) |
| 132 |
I am another foo |
| 133 |
>>> IFoo0.providedBy(s) |
| 134 |
True |
| 135 |
>>> IFoo2.providedBy(s) |
| 136 |
True |
| 137 |
|
| 138 |
|
| 139 |
.. toctree:: |
| 140 |
|
| 141 |
self |
| 142 |
Contributing <CONTRIBUTING> |
| 143 |
News <NEWS> |