Skip to content

Commit 3afc5fc

Browse files
author
Sakis Kasampalis
committed
merging pull request after fixing a few bugs
2 parents 5730bd5 + 55aa903 commit 3afc5fc

File tree

7 files changed

+96
-27
lines changed

7 files changed

+96
-27
lines changed

3-tier.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,22 @@ def get_product_list(self):
3636

3737
def get_product_information(self, product):
3838
product_info = self.business_logic.product_information(product)
39-
if product_info is not None:
39+
if product_info:
4040
print('PRODUCT INFORMATION:')
41-
print('Name: %s, Price: %.2f, Quantity: %d\n' %
42-
(product.title(), product_info.get('price', 0),
41+
print('Name: {0}, Price: {1:.2f}, Quantity: {2:}'.format(
42+
product.title(), product_info.get('price', 0),
4343
product_info.get('quantity', 0)))
4444
else:
45-
print('That product "%s" does not exist in the records' % product)
45+
print('That product "{0}" does not exist in the records'.format(product))
4646

4747

48-
if __name__ == '__main__':
49-
48+
def main():
5049
ui = Ui()
5150
ui.get_product_list()
5251
ui.get_product_information('cheese')
5352
ui.get_product_information('eggs')
5453
ui.get_product_information('milk')
5554
ui.get_product_information('arepas')
55+
56+
if __name__ == '__main__':
57+
main()

README.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
11
python-patterns
22
===============
33

4-
A collection of design patterns implemented (by other people) in python
4+
A collection of design patterns implemented (by other people) in python
5+
6+
Current Patterns:
7+
8+
* 3-tier
9+
* composite
10+
* mvc
11+
* decorator
12+
* null
13+
* facade
14+
* observer
15+
* abstract_factory
16+
* factory_method
17+
* pool
18+
* adapter
19+
* flyweight
20+
* prototype
21+
* borg
22+
* proxy
23+
* bridge
24+
* graph_search
25+
* state
26+
* builder
27+
* iterator
28+
* strategy
29+
* chain
30+
* mediator
31+
* template
32+
* command
33+
* memento
34+
* visitor

adapter.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self):
3232
self.name = "Car"
3333

3434
def make_noise(self, octane_level):
35-
return "vroom%s" % ("!" * octane_level)
35+
return "vroom{0}".format("!" * octane_level)
3636

3737

3838
class Adapter(object):
@@ -70,6 +70,21 @@ def __getattr__(self, attr):
7070
return getattr(self.obj, attr)
7171

7272

73+
def main():
74+
objects = []
75+
dog = Dog()
76+
objects.append(Adapter(dog, dict(make_noise=dog.bark)))
77+
cat = Cat()
78+
objects.append(Adapter(cat, dict(make_noise=cat.meow)))
79+
human = Human()
80+
objects.append(Adapter(human, dict(make_noise=human.speak)))
81+
car = Car()
82+
objects.append(Adapter(car, dict(make_noise=lambda: car.make_noise(3))))
83+
84+
for obj in objects:
85+
print("A {0} goes {1}".format(obj.name, obj.make_noise()))
86+
87+
7388
if __name__ == "__main__":
7489
import doctest
7590
doctest.testmod()

builder.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# -*- coding : utf-8 -*-
33

44
"""
5-
@author: Diogenes Augusto Fernandes Herminio <diofeher@gmail.com>
6-
https://gist.github.com/420905#file_builder_python.py
5+
@author: Diogenes Augusto Fernandes Herminio <diofeher@gmail.com>
6+
https://gist.github.com/420905#file_builder_python.py
77
"""
88

99

@@ -54,7 +54,7 @@ def __init__(self):
5454
self.size = None
5555

5656
def __repr__(self):
57-
return 'Floor: %s | Size: %s' % (self.floor, self.size)
57+
return 'Floor: {0.floor} | Size: {0.size}'.format(self)
5858

5959

6060
# Client

chain.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ def __init__(self):
3939
h1.successor(h2)
4040
h2.successor(h3)
4141

42-
requests = [2, 5, 14, 22, 18, 3, 35, 27, 20]
42+
self.handlers = (h1,h2,h3)
43+
44+
def delegate(self, requests):
4345
for request in requests:
44-
h1.handle(request)
46+
self.handlers[0].handle(request)
4547

4648

4749
if __name__ == "__main__":
4850
client = Client()
51+
requests = [2, 5, 14, 22, 18, 3, 35, 27, 20]
52+
client.delegate(requests)

command.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def undo(self):
1818
os.rename(self.dest, self.src)
1919

2020

21-
if __name__ == "__main__":
21+
def main():
2222
command_stack = []
2323

2424
# commands are just pushed into the command stack
@@ -32,3 +32,6 @@ def undo(self):
3232
# and can also be undone at will
3333
for cmd in reversed(command_stack):
3434
cmd.undo()
35+
36+
if __name__ == "__main__":
37+
main()

decorator.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
# http://stackoverflow.com/questions/3118929/implementing-the-decorator-pattern-in-python
22

3-
4-
class foo(object):
5-
def f1(self):
6-
print("original f1")
7-
8-
def f2(self):
9-
print("original f2")
10-
11-
123
class foo_decorator(object):
134
def __init__(self, decoratee):
145
self._decoratee = decoratee
@@ -20,7 +11,31 @@ def f1(self):
2011
def __getattr__(self, name):
2112
return getattr(self._decoratee, name)
2213

23-
u = foo()
24-
v = foo_decorator(u)
25-
v.f1()
26-
v.f2()
14+
class undecorated_foo(object):
15+
def f1(self):
16+
print("original f1")
17+
18+
def f2(self):
19+
print("original f2")
20+
21+
@foo_decorator
22+
class decorated_foo(object):
23+
def f1(self):
24+
print("original f1")
25+
26+
def f2(self):
27+
print("original f2")
28+
29+
30+
def main():
31+
u = undecorated_foo()
32+
v = foo_decorator(u)
33+
# The @foo_decorator syntax is just shorthand for calling
34+
# foo_decorator on the decorated object right after its
35+
# declaration.
36+
37+
v.f1()
38+
v.f2()
39+
40+
if __name__ == '__main__':
41+
main()

0 commit comments

Comments
 (0)