Browse Source

merging pull request after fixing a few bugs

Sakis Kasampalis 12 years ago
parent
commit
3afc5fce47
7 changed files with 96 additions and 27 deletions
  1. 8 6
      3-tier.py
  2. 31 1
      README.md
  3. 16 1
      adapter.py
  4. 3 3
      builder.py
  5. 6 2
      chain.py
  6. 4 1
      command.py
  7. 28 13
      decorator.py

+ 8 - 6
3-tier.py

@@ -36,20 +36,22 @@ class Ui(object):
 
     def get_product_information(self, product):
         product_info = self.business_logic.product_information(product)
-        if product_info is not None:
+        if product_info:
             print('PRODUCT INFORMATION:')
-            print('Name: %s, Price: %.2f, Quantity: %d\n' %
-                  (product.title(), product_info.get('price', 0),
+            print('Name: {0}, Price: {1:.2f}, Quantity: {2:}'.format(
+                   product.title(), product_info.get('price', 0),
                    product_info.get('quantity', 0)))
         else:
-            print('That product "%s" does not exist in the records' % product)
+            print('That product "{0}" does not exist in the records'.format(product))
 
 
-if __name__ == '__main__':
-
+def main():
     ui = Ui()
     ui.get_product_list()
     ui.get_product_information('cheese')
     ui.get_product_information('eggs')
     ui.get_product_information('milk')
     ui.get_product_information('arepas')
+
+if __name__ == '__main__':
+    main()

+ 31 - 1
README.md

@@ -1,4 +1,34 @@
 python-patterns
 ===============
 
-A collection of design patterns implemented (by other people) in python
+A collection of design patterns implemented (by other people) in python
+
+Current Patterns:
+
+* 3-tier		
+* composite		
+* mvc
+* decorator		
+* null
+* facade		
+* observer
+* abstract_factory	
+* factory_method	
+* pool
+* adapter		
+* flyweight		
+* prototype
+* borg					
+* proxy
+* bridge		
+* graph_search		
+* state
+* builder		
+* iterator		
+* strategy
+* chain		
+* mediator		
+* template
+* command		
+* memento		
+* visitor

+ 16 - 1
adapter.py

@@ -32,7 +32,7 @@ class Car(object):
         self.name = "Car"
 
     def make_noise(self, octane_level):
-        return "vroom%s" % ("!" * octane_level)
+        return "vroom{0}".format("!" * octane_level)
 
 
 class Adapter(object):
@@ -70,6 +70,21 @@ class Adapter(object):
         return getattr(self.obj, attr)
 
 
+def main():
+    objects = []
+    dog = Dog()
+    objects.append(Adapter(dog, dict(make_noise=dog.bark)))
+    cat = Cat()
+    objects.append(Adapter(cat, dict(make_noise=cat.meow)))
+    human = Human()
+    objects.append(Adapter(human, dict(make_noise=human.speak)))
+    car = Car()
+    objects.append(Adapter(car, dict(make_noise=lambda: car.make_noise(3))))
+
+    for obj in objects:
+        print("A {0} goes {1}".format(obj.name, obj.make_noise()))
+
+
 if __name__ == "__main__":
     import doctest
     doctest.testmod()

+ 3 - 3
builder.py

@@ -2,8 +2,8 @@
 # -*- coding : utf-8 -*-
 
 """
-    @author: Diogenes Augusto Fernandes Herminio <diofeher@gmail.com>
-    https://gist.github.com/420905#file_builder_python.py
+@author: Diogenes Augusto Fernandes Herminio <diofeher@gmail.com>
+https://gist.github.com/420905#file_builder_python.py
 """
 
 
@@ -54,7 +54,7 @@ class Building(object):
         self.size = None
 
     def __repr__(self):
-        return 'Floor: %s | Size: %s' % (self.floor, self.size)
+        return 'Floor: {0.floor} | Size: {0.size}'.format(self)
 
 
 # Client

+ 6 - 2
chain.py

@@ -39,10 +39,14 @@ class Client:
         h1.successor(h2)
         h2.successor(h3)
 
-        requests = [2, 5, 14, 22, 18, 3, 35, 27, 20]
+        self.handlers = (h1,h2,h3)
+
+    def delegate(self, requests):
         for request in requests:
-            h1.handle(request)
+            self.handlers[0].handle(request)
 
 
 if __name__ == "__main__":
     client = Client()
+    requests = [2, 5, 14, 22, 18, 3, 35, 27, 20]
+    client.delegate(requests)

+ 4 - 1
command.py

@@ -18,7 +18,7 @@ class MoveFileCommand(object):
         os.rename(self.dest, self.src)
 
 
-if __name__ == "__main__":
+def main():
     command_stack = []
 
     # commands are just pushed into the command stack
@@ -32,3 +32,6 @@ if __name__ == "__main__":
     # and can also be undone at will
     for cmd in reversed(command_stack):
         cmd.undo()
+
+if __name__ == "__main__":
+    main()

+ 28 - 13
decorator.py

@@ -1,14 +1,5 @@
 # http://stackoverflow.com/questions/3118929/implementing-the-decorator-pattern-in-python
 
-
-class foo(object):
-    def f1(self):
-        print("original f1")
-
-    def f2(self):
-        print("original f2")
-
-
 class foo_decorator(object):
     def __init__(self, decoratee):
         self._decoratee = decoratee
@@ -20,7 +11,31 @@ class foo_decorator(object):
     def __getattr__(self, name):
         return getattr(self._decoratee, name)
 
-u = foo()
-v = foo_decorator(u)
-v.f1()
-v.f2()
+class undecorated_foo(object):
+    def f1(self):
+        print("original f1")
+
+    def f2(self):
+        print("original f2")
+
+@foo_decorator
+class decorated_foo(object):
+    def f1(self):
+        print("original f1")
+
+    def f2(self):
+        print("original f2")
+
+
+def main():
+    u = undecorated_foo()
+    v = foo_decorator(u)
+    # The @foo_decorator syntax is just shorthand for calling
+    # foo_decorator on the decorated object right after its
+    # declaration.
+
+    v.f1()
+    v.f2()
+
+if __name__ == '__main__':
+    main()