Browse Source

Merge branch 'duythinht-master'

Sakis Kasampalis 11 years ago
parent
commit
b655bf082f
2 changed files with 34 additions and 0 deletions
  1. 1 0
      README.md
  2. 33 0
      chaining_method.py

+ 1 - 0
README.md

@@ -38,3 +38,4 @@ Current Patterns:
 | [strategy](strategy.py) | selectable operations over the same data |
 | [template](template.py) | an object imposes a structure but takes pluggable components |
 | [visitor](visitor.py) | invoke a callback for all items of a collection |
+| [chaining_method](chaining_method.py) | continue callback next object method |

+ 33 - 0
chaining_method.py

@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+class Person(object):
+
+    def __init__(self, name, action):
+        self.name = name
+        self.action = action
+
+    def do_action(self):
+        print(self.name, self.action.name, end=' ')
+        return self.action
+
+class Action(object):
+
+    def __init__(self, name):
+        self.name = name
+
+    def amount(self, val):
+        print(val, end=' ')
+        return self
+
+    def stop(self):
+        print('then stop')
+
+if __name__ == '__main__':
+
+    move = Action('move')
+    person = Person('Jack', move)
+    person.do_action().amount('5m').stop()
+
+### OUTPUT ###
+# Jack move 5m then stop