forked from kelvins/design-patterns-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
73 lines (47 loc) · 1.43 KB
/
example.py
File metadata and controls
73 lines (47 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from abc import ABCMeta, abstractmethod
class Payment(object):
__metaclass__ = ABCMeta
@abstractmethod
def do_pay(self):
pass
class Bank(Payment):
def __init__(self):
self.card = None
self.account = None
def __get_account(self):
self.account = self.card
return self.account
def __has_founds(self):
print("Bank:: Checking if Account %d has enough funds" % self.__get_account())
return True
def set_card(self, card):
self.card = card
def do_pay(self):
if self.__has_founds():
print("Bank:: Paying the merchant")
return True
else:
print("Bank:: Sorry, not enough funds")
return False
class DebitCard(Payment):
def __init__(self):
self.bank = Bank()
def do_pay(self):
card = input("Proxy:: Punch in Card Number: ")
self.bank.set_card(card)
return self.bank.do_pay()
class You(object):
def __init__(self):
print("You:: Lets buy the Denim shirt!")
self.debit_card = DebitCard()
self.is_purchased = None
def make_payment(self):
self.is_purchased = self.debit_card.do_pay()
def __del__(self):
if self.is_purchased:
print("You:: Wow! Denim shirt is mine :-)")
else:
print("You:: I should earn more :(")
if __name__ == "__main__":
you = You()
you.make_payment()