Skip to content

Commit 02a45af

Browse files
author
Sakis Kasampalis
committed
Merge branch 'master' of https://github.com/harenson/python-patterns into harenson-master
2 parents 864f73f + 88fd254 commit 02a45af

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

3-tier.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
class Data(object):
5+
6+
products = {
7+
'milk': { 'price': 1.50, 'quantity': 10 },
8+
'eggs': { 'price': 0.20, 'quantity': 100 },
9+
'cheese': { 'price': 2.00, 'quantity': 10 }
10+
}
11+
12+
13+
class BusinessLogic(object):
14+
15+
def __init__(self):
16+
self.data = Data()
17+
18+
def product_list(self):
19+
return self.data.products.keys()
20+
21+
def product_information(self, product):
22+
return self.data.products.get(product, None)
23+
24+
25+
class Ui(object):
26+
27+
def __init__(self):
28+
self.business_logic = BusinessLogic()
29+
30+
def get_product_list(self):
31+
print('PRODUCT LIST:')
32+
for product in self.business_logic.product_list():
33+
print(product)
34+
print('')
35+
36+
def get_product_information(self, product):
37+
product_info = self.business_logic.product_information(product)
38+
if product_info is not None:
39+
print('PRODUCT INFORMATION:')
40+
print('Name: %s, Price: %.2f, Quantity: %d\n' % \
41+
(product.title(), product_info.get('price', 0), \
42+
product_info.get('quantity', 0)))
43+
else:
44+
print('That product "%s" does not exist in the records' % product)
45+
46+
47+
if __name__ == '__main__':
48+
49+
ui = Ui()
50+
ui.get_product_list()
51+
ui.get_product_information('cheese')
52+
ui.get_product_information('eggs')
53+
ui.get_product_information('milk')
54+
ui.get_product_information('arepas')

mvc.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
class Model(object):
5+
6+
products = {
7+
'milk': { 'price': 1.50, 'quantity': 10 },
8+
'eggs': { 'price': 0.20, 'quantity': 100 },
9+
'cheese': { 'price': 2.00, 'quantity': 10 }
10+
}
11+
12+
13+
class View(object):
14+
15+
def product_list(self, product_list):
16+
print('PRODUCT LIST:')
17+
for product in product_list:
18+
print(product)
19+
print('')
20+
21+
def product_information(self, product, product_info):
22+
print('PRODUCT INFORMATION:')
23+
print('Name: %s, Price: %.2f, Quantity: %d\n' % \
24+
(product.title(), product_info.get('price', 0), \
25+
product_info.get('quantity', 0)))
26+
27+
def product_not_found(self, product):
28+
print('That product "%s" does not exist in the records' % product)
29+
30+
31+
class Controller(object):
32+
33+
def __init__(self):
34+
self.model = Model()
35+
self.view = View()
36+
37+
def get_product_list(self):
38+
product_list = self.model.products.keys()
39+
self.view.product_list(product_list)
40+
41+
def get_product_information(self, product):
42+
product_info = self.model.products.get(product, None)
43+
if product_info is not None:
44+
self.view.product_information(product, product_info)
45+
else:
46+
self.view.product_not_found(product)
47+
48+
49+
if __name__ == '__main__':
50+
51+
controller = Controller()
52+
controller.get_product_list()
53+
controller.get_product_information('cheese')
54+
controller.get_product_information('eggs')
55+
controller.get_product_information('milk')
56+
controller.get_product_information('arepas')

0 commit comments

Comments
 (0)