-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_AbstractFactory.py
73 lines (48 loc) · 1.85 KB
/
1_AbstractFactory.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
class Text(object):
def __init__(self, x, y, text, fontsize):
self.x = x
self.y = y
self.rows = [list(text)]
class Diagram(object):
# example skipping of constructor
def add(self, component):
for y, row in enumerate(component.rows):
for x, char in enumerate(row):
self.diagram[y + component.y][x + component.x] = char
SVG_TEXT = """<text x="{x}" y="{y}" text-anchor="left" font-family="sans-serif" font-size="{fontsize}">{text}</text>"""
SVG_SCALE = 20
class SvgText(object):
def __init__(self, x, y, text, fontsize):
x *= SVG_SCALE
y *= SVG_SCALE
fontsize *= SVG_SCALE // 10
# turns into x=x, y=y, etc
self.svg = SVG_TEXT.format(**locals())
class DiagramFactory(object):
def make_diagram(self, w, h):
return Diagram(w, h)
def make_rectangle(self, x, y, w, h, fill="white", stroke="black"):
return Rectangle(x, y, w, h, fill, stroke)
def make_text(self, x, y, text, fontsize=12):
return Text(x, y, text, fontsize)
class SvgDiagramFactory(object):
def make_diagram(self, w, h):
return SvgDiagram(w, h)
def make_rectangle(self, x, y, w, h, fill="white", stroke="black"):
return SvgRectangle(x, y, w, h, fill, stroke)
def make_text(self, x, y, text, fontsize=12):
return SvgText(x, y, text, fontsize)
def main():
txt_diagram = create_diagram(DiagramFactory())
txt_diagram.save(text_filename)
svg_diagram = create_diagram(SvgDiagramFactory())
svg_diagram.save(svg_filename)
def create_diagram(factory):
diagram = factory.make_diagram(30, 7)
rectangle = factory.make_rectangle(4, 1, 22, 5, "yellow")
text = factory.make_text(7, 3, "Abstract Factory")
diagram.add(rectangle)
diagram.add(text)
return diagram
if __name__ == '__main__':
main()