Skip to content

Commit 99f7128

Browse files
committed
TR updates, first round
1 parent d025459 commit 99f7128

File tree

3 files changed

+17
-46
lines changed

3 files changed

+17
-46
lines changed

solid-principles-python/app_dip.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
# Bad example
44
# class Frontend:
5-
# """Frontend, concrete implementation."""
6-
75
# def __init__(self, backend):
86
# self.backend = backend
97

@@ -13,17 +11,13 @@
1311

1412

1513
# class Backend:
16-
# """Backend, concrete implementation."""
17-
1814
# def get_data_from_database(self):
1915
# """Return data from the database."""
2016
# return "Data from the database"
2117

2218

2319
# Good example
2420
class Frontend:
25-
"""Frontend, concrete implementation."""
26-
2721
def __init__(self, data_source):
2822
self.data_source = data_source
2923

@@ -33,24 +27,16 @@ def display_data(self):
3327

3428

3529
class DataSource(ABC):
36-
"""DataSource interface (Abstraction)."""
37-
3830
@abstractmethod
3931
def get_data(self):
4032
pass
4133

4234

4335
class Database(DataSource):
44-
"""Database, concrete implementation."""
45-
4636
def get_data(self):
47-
"""Return data from the database."""
4837
return "Data from the database"
4938

5039

5140
class API(DataSource):
52-
"""API, concrete implementation."""
53-
5441
def get_data(self):
55-
"""Return data from the API."""
5642
return "Data from the API"

solid-principles-python/printers_isp.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
# Bad example
44
# class Printer(ABC):
5-
# """Printer interface."""
6-
75
# @abstractmethod
86
# def print(self, document):
97
# pass
@@ -18,10 +16,8 @@
1816

1917

2018
# class OldPrinter(Printer):
21-
# """Old printer, concrete implementation."""
22-
2319
# def print(self, document):
24-
# print(f"Print {document} in black and white")
20+
# print(f"Printing {document} in black and white...")
2521

2622
# def fax(self, document):
2723
# raise NotImplementedError("Fax functionality not supported")
@@ -31,59 +27,46 @@
3127

3228

3329
# class ModernPrinter(Printer):
34-
# """Modern printer, concrete implementation."""
35-
3630
# def print(self, document):
37-
# print(f"Printing {document} in color")
31+
# print(f"Printing {document} in color...")
3832

3933
# def fax(self, document):
40-
# print(f"Faxing {document}")
34+
# print(f"Faxing {document}...")
4135

4236
# def scan(self, document):
43-
# print(f"Scanning {document}")
37+
# print(f"Scanning {document}...")
4438

4539

4640
# Good example
4741
class Printer(ABC):
48-
"""Printer interface."""
49-
5042
@abstractmethod
5143
def print(self, document):
5244
pass
5345

5446

5547
class Fax(ABC):
56-
"""Fax interface."""
57-
5848
@abstractmethod
5949
def fax(self, document):
6050
pass
6151

6252

63-
class Scan(ABC):
64-
"""Scan interface."""
65-
53+
class Scanner(ABC):
6654
@abstractmethod
6755
def scan(self, document):
6856
pass
6957

7058

7159
class OldPrinter(Printer):
72-
"""Old printer, concrete implementation."""
73-
7460
def print(self, document):
75-
print(f"Print {document} in black and white")
76-
61+
print(f"Printing {document} in black and white...")
7762

78-
class NewPrinter(Printer, Fax, Scan):
79-
"""New printer, concrete implementation."""
8063

64+
class NewPrinter(Printer, Fax, Scanner):
8165
def print(self, document):
82-
print(f"Printing {document} in color")
66+
print(f"Printing {document} in color...")
8367

8468
def fax(self, document):
85-
print(f"Faxing {document}")
69+
print(f"Faxing {document}...")
8670

8771
def scan(self, document):
88-
print(f"Scanning {document}")
89-
print(f"Scanning {document}")
72+
print(f"Scanning {document}...")

solid-principles-python/shapes_ocp.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from abc import ABC, abstractmethod
22
from math import pi
33

4+
# Bad example
45
# class Shape:
56
# def __init__(self, shape_type, **kwargs):
67
# self.shape_type = shape_type
@@ -10,19 +11,20 @@
1011
# elif self.shape_type == "circle":
1112
# self.radius = kwargs["radius"]
1213

13-
# def area(self):
14+
# def calculate_area(self):
1415
# if self.shape_type == "rectangle":
1516
# return self.width * self.height
1617
# elif self.shape_type == "circle":
1718
# return pi * self.radius**2
1819

1920

21+
# Good example
2022
class Shape(ABC):
2123
def __init__(self, shape_type):
2224
self.shape_type = shape_type
2325

2426
@abstractmethod
25-
def area(self):
27+
def calculate_area(self):
2628
pass
2729

2830

@@ -31,7 +33,7 @@ def __init__(self, radius):
3133
super().__init__("circle")
3234
self.radius = radius
3335

34-
def area(self):
36+
def calculate_area(self):
3537
return pi * self.radius**2
3638

3739

@@ -41,7 +43,7 @@ def __init__(self, width, height):
4143
self.width = width
4244
self.height = height
4345

44-
def area(self):
46+
def calculate_area(self):
4547
return self.width * self.height
4648

4749

@@ -50,5 +52,5 @@ def __init__(self, side):
5052
super().__init__("square")
5153
self.side = side
5254

53-
def area(self):
55+
def calculate_area(self):
5456
return self.side**2

0 commit comments

Comments
 (0)