new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
25
Task/Delegates/Python/delegates.py
Normal file
25
Task/Delegates/Python/delegates.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
class Delegator:
|
||||
def __init__(self):
|
||||
self.delegate = None
|
||||
def operation(self):
|
||||
if hasattr(self.delegate, 'thing') and callable(self.delegate.thing):
|
||||
return self.delegate.thing()
|
||||
return 'default implementation'
|
||||
|
||||
class Delegate:
|
||||
def thing(self):
|
||||
return 'delegate implementation'
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
# No delegate
|
||||
a = Delegator()
|
||||
assert a.operation() == 'default implementation'
|
||||
|
||||
# With a delegate that does not implement "thing"
|
||||
a.delegate = 'A delegate may be any object'
|
||||
assert a.operation() == 'default implementation'
|
||||
|
||||
# With delegate that implements "thing"
|
||||
a.delegate = Delegate()
|
||||
assert a.operation() == 'delegate implementation'
|
||||
Loading…
Add table
Add a link
Reference in a new issue