|
| 1 | +# pylint: disable=missing-class-docstring,pointless-string-statement,missing-function-docstring,broad-exception-raised,too-few-public-methods,missing-module-docstring |
| 2 | + |
| 3 | +from abc import ABCMeta, abstractmethod |
| 4 | + |
| 5 | +""" |
| 6 | + Interface Segregation Principle (ISP)... |
| 7 | +""" |
| 8 | + |
| 9 | +print("Interface Segregation Principle (ISP)...") |
| 10 | + |
| 11 | + |
| 12 | +class AbcBadWorker(metaclass=ABCMeta): |
| 13 | + @abstractmethod |
| 14 | + def eat(self) -> None: |
| 15 | + pass |
| 16 | + |
| 17 | + @abstractmethod |
| 18 | + def work(self) -> None: |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +class BadProgrammer(AbcBadWorker): |
| 23 | + def eat(self) -> None: |
| 24 | + print("Eating!") |
| 25 | + |
| 26 | + def work(self) -> None: |
| 27 | + print("Working!") |
| 28 | + |
| 29 | + |
| 30 | +class BadRobot(AbcBadWorker): |
| 31 | + def eat(self) -> None: |
| 32 | + raise Exception("A robot can not eat") |
| 33 | + |
| 34 | + def work(self) -> None: |
| 35 | + print("Working!") |
| 36 | + |
| 37 | + |
| 38 | +print("\nBad implementation of Interface Segregation Principle (ISP)...") |
| 39 | + |
| 40 | +print( |
| 41 | + "\n```", |
| 42 | + """class AbcBadWorker(metaclass=ABCMeta): |
| 43 | + @abstractmethod |
| 44 | + def eat(self) -> None: |
| 45 | + pass |
| 46 | +
|
| 47 | + @abstractmethod |
| 48 | + def work(self) -> None: |
| 49 | + pass |
| 50 | +
|
| 51 | +
|
| 52 | +class BadProgrammer(AbcBadWorker): |
| 53 | + def eat(self) -> None: |
| 54 | + print("Eating!") |
| 55 | +
|
| 56 | + def work(self) -> None: |
| 57 | + print("Working!") |
| 58 | +
|
| 59 | +
|
| 60 | +class BadRobot(AbcBadWorker): |
| 61 | + def eat(self) -> None: |
| 62 | + raise Exception("A robot can not eat") |
| 63 | +
|
| 64 | + def work(self) -> None: |
| 65 | + print("Working!")""", |
| 66 | + "```", |
| 67 | + sep="\n", |
| 68 | +) |
| 69 | + |
| 70 | +print( |
| 71 | + "\nThis is a bad implementation of Interface Segregation Principle (ISP),", |
| 72 | + "because the 'BadRobot' class should not implement an interface with methods", |
| 73 | + "that it is going to never use. So, the implemented interface ('AbcBadWorker')", |
| 74 | + "is to general for the 'BadRobot' class, but perfect for the 'BadProgrammer' class.", |
| 75 | + sep="\n", |
| 76 | +) |
| 77 | + |
| 78 | +print("\nGood implementation of Interface Segregation Principle (ISP)...") |
| 79 | + |
| 80 | + |
| 81 | +class AbcHumanWorker(metaclass=ABCMeta): |
| 82 | + @abstractmethod |
| 83 | + def eat(self) -> None: |
| 84 | + pass |
| 85 | + |
| 86 | + @abstractmethod |
| 87 | + def work(self) -> None: |
| 88 | + pass |
| 89 | + |
| 90 | + |
| 91 | +class AbcNotHumanWorker(metaclass=ABCMeta): |
| 92 | + @abstractmethod |
| 93 | + def work(self) -> None: |
| 94 | + pass |
| 95 | + |
| 96 | + |
| 97 | +class GoodProgrammer(AbcHumanWorker): |
| 98 | + def eat(self) -> None: |
| 99 | + print("Eating!") |
| 100 | + |
| 101 | + def work(self) -> None: |
| 102 | + print("Working!") |
| 103 | + |
| 104 | + |
| 105 | +class GoodRobot(AbcNotHumanWorker): |
| 106 | + def work(self) -> None: |
| 107 | + print("Working!") |
| 108 | + |
| 109 | + |
| 110 | +print( |
| 111 | + "\n```", |
| 112 | + """class AbcHumanWorker(metaclass=ABCMeta): |
| 113 | + @abstractmethod |
| 114 | + def eat(self) -> None: |
| 115 | + pass |
| 116 | +
|
| 117 | + @abstractmethod |
| 118 | + def work(self) -> None: |
| 119 | + pass |
| 120 | +
|
| 121 | +
|
| 122 | +class AbcNotHumanWorker(metaclass=ABCMeta): |
| 123 | + @abstractmethod |
| 124 | + def work(self) -> None: |
| 125 | + pass |
| 126 | +
|
| 127 | +
|
| 128 | +class GoodProgrammer(AbcHumanWorker): |
| 129 | + def eat(self) -> None: |
| 130 | + print("Eating!") |
| 131 | +
|
| 132 | + def work(self) -> None: |
| 133 | + print("Working!") |
| 134 | +
|
| 135 | +
|
| 136 | +class GoodRobot(AbcNotHumanWorker): |
| 137 | + def work(self) -> None: |
| 138 | + print("Working!")""", |
| 139 | + "```", |
| 140 | + sep="\n", |
| 141 | +) |
| 142 | + |
| 143 | +print( |
| 144 | + "\nThis is a good implementation of Interface Segregation Principle (ISP),", |
| 145 | + "because the 'GoodRobot' class only implements the necessary methods, without", |
| 146 | + "any extras. Also, the 'GoodProgrammer' class utilizes an interface perfectly", |
| 147 | + "suited to its needs, with no extra methods.", |
| 148 | + sep="\n", |
| 149 | +) |
| 150 | + |
| 151 | +print( |
| 152 | + "\n# ---------------------------------------------------------------------------------- #\n" |
| 153 | +) |
| 154 | + |
| 155 | +""" |
| 156 | + Additional challenge... |
| 157 | +""" |
| 158 | + |
| 159 | +print("Additional challenge...") |
| 160 | + |
| 161 | + |
| 162 | +class AbcBlackAndWhitePrinter(metaclass=ABCMeta): |
| 163 | + @abstractmethod |
| 164 | + def print_(self) -> None: |
| 165 | + pass |
| 166 | + |
| 167 | + |
| 168 | +class BlackAndWhitePrinter(AbcBlackAndWhitePrinter): |
| 169 | + def print_(self) -> None: |
| 170 | + print("Paper printed in black and white!") |
| 171 | + |
| 172 | + |
| 173 | +class AbcColorPrinter(metaclass=ABCMeta): |
| 174 | + @abstractmethod |
| 175 | + def print_(self) -> None: |
| 176 | + pass |
| 177 | + |
| 178 | + |
| 179 | +class ColorPrinter(AbcColorPrinter): |
| 180 | + def print_(self) -> None: |
| 181 | + print("Paper printed in color!") |
| 182 | + |
| 183 | + |
| 184 | +class AbcMultifunctionalPrinter(metaclass=ABCMeta): |
| 185 | + @abstractmethod |
| 186 | + def print_(self) -> None: |
| 187 | + pass |
| 188 | + |
| 189 | + @abstractmethod |
| 190 | + def scan(self) -> None: |
| 191 | + pass |
| 192 | + |
| 193 | + @abstractmethod |
| 194 | + def send_fax(self) -> None: |
| 195 | + pass |
| 196 | + |
| 197 | + |
| 198 | +class MultifunctionalPrinter(AbcMultifunctionalPrinter): |
| 199 | + def print_(self) -> None: |
| 200 | + print("Paper printed!") |
| 201 | + |
| 202 | + def scan(self) -> None: |
| 203 | + print("Scan completed!") |
| 204 | + |
| 205 | + def send_fax(self) -> None: |
| 206 | + print("Fax sent!") |
| 207 | + |
| 208 | + |
| 209 | +black_and_white_printer: BlackAndWhitePrinter = BlackAndWhitePrinter() |
| 210 | +color_printer: ColorPrinter = ColorPrinter() |
| 211 | +multifunctional_printer: MultifunctionalPrinter = MultifunctionalPrinter() |
| 212 | + |
| 213 | +print() |
| 214 | +black_and_white_printer.print_() |
| 215 | + |
| 216 | +print() |
| 217 | +color_printer.print_() |
| 218 | + |
| 219 | +print() |
| 220 | +multifunctional_printer.print_() |
| 221 | + |
| 222 | +print() |
| 223 | +multifunctional_printer.scan() |
| 224 | + |
| 225 | +print() |
| 226 | +multifunctional_printer.send_fax() |
0 commit comments