Delegates: Difference between revisions
Content deleted Content added
Put python back |
Update Python to simplified task |
||
Line 82: | Line 82: | ||
<pre> |
<pre> |
||
class Delegator: |
class Delegator: |
||
def __init__(self |
def __init__(self): |
||
self.delegate = |
self.delegate = None |
||
def |
def operation(self): |
||
if hasattr(self.delegate, 'thing'): |
if hasattr(self.delegate, 'thing'): |
||
return self.delegate.thing() |
return self.delegate.thing() |
||
return |
return 'default implementation' |
||
def other(self): |
|||
if hasattr(self.delegate, 'other'): |
|||
return self.delegate.thing() |
|||
return 5 |
|||
class Delegate: |
class Delegate: |
||
def thing(self): |
def thing(self): |
||
return |
return 'delegate implementation' |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
Line 101: | Line 97: | ||
# No delegate |
# No delegate |
||
a = Delegator() |
a = Delegator() |
||
assert a. |
assert a.operation() == 'default implementation' |
||
assert a.other() == 5 |
|||
# With delegate |
# With delegate |
||
a.delegate = Delegate() |
a.delegate = Delegate() |
||
assert a. |
assert a.operation() == 'delegate implementation' |
||
assert a.other() == 5 |
|||
</pre> |
</pre> |