-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathisp_comply.py
More file actions
86 lines (57 loc) · 2.25 KB
/
isp_comply.py
File metadata and controls
86 lines (57 loc) · 2.25 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
"""An example showing compliance to Interface Segregation Principle.
Example based on https://www.udemy.com/course/design-patterns-python/.
"""
from abc import abstractmethod, ABCMeta
from isp.isp_common import Document
# Instead of having a large interface, we can split it into small interfaces so that
# people can implement what they need.
class Printer(metaclass=ABCMeta):
"""Interface for a machine that can print documents."""
@abstractmethod
def print(self, document: Document) -> None:
"""Print a document."""
class Scanner(metaclass=ABCMeta):
"""Interface for a machine that can scan documents."""
@abstractmethod
def scan(self, document: Document) -> None:
"""Scan a document."""
class Facsimile(metaclass=ABCMeta):
"""Interface for a machine that can fax documents."""
@abstractmethod
def fax(self, document: Document) -> None:
"""Fax a document."""
class PrintOnlyPrinter(Printer):
"""A printer that only prints."""
def print(self, document: Document) -> None:
"""Print a document."""
document.printed = True
class Photocopier(Printer, Scanner):
"""A photocopier scans and prints documents."""
def print(self, document: Document) -> None:
"""Print a document."""
document.printed = True
def scan(self, document: Document) -> None:
"""Scan a document."""
document.scanned = True
class MultiFunction(Printer, Scanner, Facsimile):
"""If you really need an interface with all the different methods."""
@abstractmethod
def print(self, document: Document) -> None:
"""Print a document."""
@abstractmethod
def scan(self, document: Document) -> None:
"""Scan a document."""
@abstractmethod
def fax(self, document: Document) -> None:
"""Fax a document."""
class MultiFunctionMachine(MultiFunction):
"""This is the same as `isp_violate.MultiFunctionPrinter`."""
def print(self, document: Document) -> None:
"""Print a document."""
document.printed = True
def fax(self, document: Document) -> None:
"""Fax a document."""
document.faxed = True
def scan(self, document: Document) -> None:
"""Scan a document."""
document.scanned = True