forked from abhi1540/PythonConceptExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliskovsubstitution.py
More file actions
92 lines (53 loc) · 2.22 KB
/
liskovsubstitution.py
File metadata and controls
92 lines (53 loc) · 2.22 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# https://lassala.net/2010/11/04/a-good-example-of-liskov-substitution-principle/
class IPersistedResource:
def load(self):
pass
def persist(self):
pass
class ApplicationSettings(IPersistedResource):
def load(self):
print("This is for application load settings....")
def persist(self):
print("This is for application persist settings.....")
class UserSettings(IPersistedResource):
def load(self):
print("This is for user load settingss....")
def persist(self):
print("This is for user persist settings...")
class NewSettings(IPersistedResource):
def load(self):
print("This is for new load settings...")
def persist(self):
raise NotImplementedError
# newsettings = NewSettings()
# newsettings.load()
# newsettings.persist()
# it is clear that "NewSettings" is NOT substitutable by its
# "IPersistedResource" interface; if we call Persist on it, the app blows up, so we need change the method
# to take that one problem into consideration. One could say “well, let’s change the Persist method on that
# class so it won’t throw an exception anymore”. Hmm, having a method on a class that when called won’t do
# what its name implies is just bad… really, really bad.
# Write this down: anytime you see code that takes in some sort of baseclass or interface and then
# performs a check such as “if (someObject is SomeType)”, there’s a very good chance that that’s an
# LSP violation. I’ve done that, and I know so have you, let’s be honest.
"""To fix this we will create separate class for each method"""
class ILoadResource:
def load(self):
pass
class IPersistedResource:
def persist(self):
pass
# when you implement this NewSettings only inherite required class
class NewSettings(ILoadResource):
def load(self):
print("This is for new load settings...")
class UserSettings(ILoadResource, IPersistedResource):
def load(self):
print("This is for user load settingss....")
def persist(self):
print("This is for user persist settings...")
newsettings = NewSettings()
newsettings.load()
usersettings = UserSettings()
usersettings.persist()
usersettings.load()