Skip to content

Commit 182f8c5

Browse files
Exercise (source) for Python Design Patterns
1 parent ecf9a24 commit 182f8c5

File tree

22 files changed

+390
-0
lines changed

22 files changed

+390
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CodeBuilder:
2+
def __init__(self, root_name):javascript:void(0)
3+
# todo
4+
5+
def add_field(self, type, name):
6+
# todo
7+
8+
def __str__(self):
9+
# todo
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Person:
2+
def __init__(self, id, name):
3+
self.id = id
4+
self.name = name
5+
6+
class PersonFactory:
7+
def create_person(self, name):
8+
# todo
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Point:
2+
def __init__(self, x=0, y=0):
3+
self.x = x
4+
self.y = y
5+
6+
class Line:
7+
def __init__(self, start=Point(), end=Point()):
8+
self.start = start
9+
self.end = end
10+
11+
def deep_copy(self):
12+
# TODO
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def is_singleton(factory):
2+
# todo: call factory() and return true or false
3+
# depending on whether the factory makes a
4+
# singleton or not
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Square:
2+
def __init__(self, side=0):
3+
self.side = side
4+
5+
def calculate_area(rc):
6+
return rc.width * rc.height
7+
8+
class SquareToRectangleAdapter:
9+
def __init__(self, square):
10+
# TODO
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# class Shape:
2+
# def __init__(self):
3+
# self.name = None
4+
#
5+
#
6+
# class Triangle(Shape):
7+
# def __init__(self):
8+
# super().__init__()
9+
# self.name = 'Triangle'
10+
#
11+
#
12+
# class Square(Shape):
13+
# def __init__(self):
14+
# super().__init__()
15+
# self.name = 'Square'
16+
#
17+
#
18+
# class VectorSquare(Square):
19+
# def __str__(self):
20+
# return f'Drawing {self.name} as lines'
21+
#
22+
#
23+
# class RasterSquare(Square):
24+
# def __str__(self):
25+
# return f'Drawing {self.name} as pixels'
26+
27+
# imagine VectorTriangle and RasterTriangle are here too
28+
29+
class Renderer(ABC):
30+
@property
31+
def what_to_render_as(self):
32+
return None
33+
34+
# TODO: reimplement Shape, Square, Triangle and Renderer/VectorRenderer/RasterRenderer
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class SingleValue:
2+
def __init__(self, value):
3+
self.value = value
4+
5+
class ManyValues(list):
6+
pass
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Circle:
2+
def __init__(self, radius):
3+
self.radius = radius
4+
5+
def resize(self, factor):
6+
self.radius *= factor
7+
8+
def __str__(self):
9+
# todo
10+
11+
class Square:
12+
def __init__(self, side):
13+
self.side = side
14+
15+
def __str__(self):
16+
# todo
17+
18+
19+
class ColoredShape:
20+
def __init__(self, shape, color):
21+
self.color = color
22+
self.shape = shape
23+
24+
def resize(self, factor):
25+
# todo
26+
# note that a Square doesn't have resize()
27+
28+
def __str__(self):
29+
# todo
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from random import randint
2+
3+
class Generator:
4+
def generate(self, count):
5+
return [randint(1,9) for x in range(count)]
6+
7+
class Splitter:
8+
def split(self, array):
9+
result = []
10+
11+
row_count = len(array)
12+
col_count = len(array[0])
13+
14+
for r in range(row_count):
15+
the_row = []
16+
for c in range(col_count):
17+
the_row.append(array[r][c])
18+
result.append(the_row)
19+
20+
for c in range(col_count):
21+
the_col = []
22+
for r in range(row_count):
23+
the_col.append(array[r][c])
24+
result.append(the_col)
25+
26+
diag1 = []
27+
diag2 = []
28+
29+
for c in range(col_count):
30+
for r in range(row_count):
31+
if c == r:
32+
diag1.append(array[r][c])
33+
r2 = row_count - r - 1
34+
if c == r2:
35+
diag2.append(array[r][c])
36+
37+
result.append(diag1)
38+
result.append(diag2)
39+
40+
return result
41+
42+
class Verifier:
43+
def verify(self, arrays):
44+
first = sum(arrays[0])
45+
46+
for i in range(1, len(arrays)):
47+
if sum(arrays[i]) != first:
48+
return False
49+
50+
return True
51+
52+
class MagicSquareGenerator:
53+
def generate(self, size):
54+
# todo - return a magic square of the given size
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Sentence:
2+
def __init__(self, plain_text):
3+
# todo

0 commit comments

Comments
 (0)