-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_factory.py
93 lines (55 loc) · 1.8 KB
/
abstract_factory.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Abstract Factory Design Pattern.
Intent: Provide an interface for creating families of related or dependent objects without specifying their
concrete classes.
"""
from __future__ import annotations
from abc import ABC, abstractmethod
class Button(ABC):
@abstractmethod
def click(self):
pass
class MacOSButton(Button):
def click(self) -> str:
return "You've clicked on a MacOS button."
class WindowsButton(Button):
def click(self) -> str:
return "You've clicked on a Windows button."
class Checkbox(ABC):
@abstractmethod
def select(self):
pass
class MacOSCheckbox(Checkbox):
def select(self) -> str:
return "You've selected a MacOS checkbox."
class WindowsCheckbox(Checkbox):
def select(self) -> str:
return "You've selected a Windows checkbox."
class GUIFactory(ABC):
"""
The Abstract Factory interface declares a set of methods that return different abstract products.
These products are called a family and are related by a high-level theme or concept.
"""
@abstractmethod
def create_button(self) -> Button:
pass
@abstractmethod
def create_checkbox(self) -> Checkbox:
pass
class MacOSGUIFactory(GUIFactory):
def create_button(self) -> MacOSButton:
return MacOSButton()
def create_checkbox(self) -> MacOSCheckbox:
return MacOSCheckbox()
class WindowsGUIFactory(GUIFactory):
def create_button(self) -> WindowsButton:
return WindowsButton()
def create_checkbox(self) -> WindowsCheckbox:
return WindowsCheckbox()
def app(factory: GUIFactory):
button = factory.create_button()
checkbox = factory.create_checkbox()
print(button.click())
print(checkbox.select())
if __name__ == '__main__':
app(WindowsGUIFactory())