-
Notifications
You must be signed in to change notification settings - Fork 32
/
interface_segregation.py
93 lines (64 loc) · 1.49 KB
/
interface_segregation.py
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
93
"""
Interface Segregation Principle
- Don't put too much into an interface
- Split into separate interfaces
- YAGNI - You Aint Gonna Need It
"""
from abc import abstractmethod
# before
class Machine:
def print(self, doc):
raise NotImplementedError
def fax(self, doc):
raise NotImplementedError
def scan(self, doc):
raise NotImplementedError
class MultiFunctionPrinter(Machine):
def print(self, doc):
pass
def fax(self, doc):
pass
def scan(self, doc):
pass
class OldFashionedPrinter(Machine):
def print(self, doc):
# ok
pass
def fax(self, doc):
# cannot fax
pass
def scan(self, doc):
# cannot scan
pass
# after
class Printer:
@abstractmethod
def print(self, doc):
pass
class Scanner:
@abstractmethod
def scan(self, doc):
pass
class MyPrint(Printer):
def print(self, doc):
print(doc)
class Photocopier(Printer, Scanner):
def print(self, doc):
pass
def scan(self, doc):
pass
class MultiFunctionDevice(Printer, Scanner):
@abstractmethod
def print(self, doc):
pass
@abstractmethod
def scan(self, doc):
pass
class MultiFunctionMachine(MultiFunctionDevice):
def __init__(self, printer, scanner):
self.scanner = scanner
self.printer = printer
def print(self, doc):
self.printer.print(doc)
def scan(self, doc):
self.scanner.scan(doc)