Skip to content

Commit 5a7cc04

Browse files
committed
adding factory and abstract_factory
1 parent a7e0c9b commit 5a7cc04

10 files changed

+300
-0
lines changed
70.2 KB
Loading

abstract_factory/chair_factory.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""A Factory Pattern Example
2+
The Factory Pattern Defines in Interface for creating an object
3+
and defers instantation until runtime.
4+
Used when you don't know how many or what type of objects will be needed until during runtime
5+
"""
6+
7+
from abc import ABCMeta, abstractstaticmethod
8+
9+
10+
class IChair(metaclass=ABCMeta): # pylint: disable=too-few-public-methods
11+
"""The Chair Interface"""
12+
13+
@abstractstaticmethod
14+
def dimensions():
15+
"""A static inteface method"""
16+
17+
18+
class BigChair(IChair): # pylint: disable=too-few-public-methods
19+
"""The Big Chair Concrete Class which implements the IChair interface"""
20+
21+
def __init__(self):
22+
self._height = 80
23+
self._width = 80
24+
self._depth = 80
25+
26+
def dimensions(self):
27+
return {"width": self._width, "depth": self._depth, "height": self._height}
28+
29+
30+
class MediumChair(IChair): # pylint: disable=too-few-public-methods
31+
"""The Medium Chair Concrete Class which implements the IChair interface"""
32+
33+
def __init__(self):
34+
self._height = 60
35+
self._width = 60
36+
self._depth = 60
37+
38+
def dimensions(self):
39+
return {"width": self._width, "depth": self._depth, "height": self._height}
40+
41+
42+
class SmallChair(IChair): # pylint: disable=too-few-public-methods
43+
"""The Small Chair Concrete Class which implements the IChair interface"""
44+
45+
def __init__(self):
46+
self._height = 40
47+
self._width = 40
48+
self._depth = 40
49+
50+
def dimensions(self):
51+
return {"width": self._width, "depth": self._depth, "height": self._height}
52+
53+
54+
class ChairFactory: # pylint: disable=too-few-public-methods
55+
"""Tha Factory Class"""
56+
57+
@staticmethod
58+
def get_chair(chair):
59+
"""A static method to get a table"""
60+
try:
61+
if chair == "BigChair":
62+
return BigChair()
63+
if chair == "MediumChair":
64+
return MediumChair()
65+
if chair == "SmallChair":
66+
return SmallChair()
67+
raise AssertionError("Chair Not Found")
68+
except AssertionError as _e:
69+
print(_e)
70+
return None
71+
72+
73+
if __name__ == "__main__":
74+
CHAIR_FACTORY = ChairFactory().get_chair("SmallChair")
75+
print(CHAIR_FACTORY.dimensions())
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
digraph "classes_abstract_factory" {
2+
charset="utf-8"
3+
rankdir=BT
4+
"1" [label="{Factory1|\l|get_factory()\l}", shape="record"];
5+
"2" [label="{Factory2|\l|get_factory()\l}", shape="record"];
6+
"3" [label="{Factory3|\l|get_factory()\l}", shape="record"];
7+
"0" [label="{IFactory|\l|get_factory()\l}", shape="record"];
8+
"1" -> "0" [arrowhead="empty", arrowtail="none"];
9+
"2" -> "0" [arrowhead="empty", arrowtail="none"];
10+
"3" -> "0" [arrowhead="empty", arrowtail="none"];
11+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""An Abstract Factory Pattern Example
2+
The Abstract Factory Pattern adds an abstract layer over multiple factory method implementations.
3+
The Abstract Factory contains or composites one or more than one factory method
4+
"""
5+
6+
from abc import ABCMeta, abstractstaticmethod
7+
from chair_factory import ChairFactory
8+
from table_factory import TableFactory
9+
10+
11+
class IFurnitureFactory(metaclass=ABCMeta): # pylint: disable=too-few-public-methods
12+
"""Furniture Factory Interface"""
13+
14+
@abstractstaticmethod
15+
def get_furniture(furniture):
16+
"""The static funiture factory inteface method"""
17+
18+
19+
class FurnitureFactory(IFurnitureFactory): # pylint: disable=too-few-public-methods
20+
"""The Furniture Factory Concrete Class"""
21+
22+
@staticmethod
23+
def get_furniture(furniture):
24+
"""Static get_furniture method"""
25+
try:
26+
if furniture in ["SmallChair", "MediumChair", "BigChair"]:
27+
return ChairFactory().get_chair(furniture)
28+
if furniture in ["SmallTable", "MediumTable", "BigTable"]:
29+
return TableFactory().get_table(furniture)
30+
raise AssertionError("No Furniture Factory Found")
31+
except AssertionError as _e:
32+
print(_e)
33+
return None
34+
35+
36+
FURNITURE = FurnitureFactory.get_furniture("SmallChair")
37+
print(f"{FURNITURE.__class__} : {FURNITURE.dimensions()}")
38+
39+
FURNITURE = FurnitureFactory.get_furniture("MediumTable")
40+
print(f"{FURNITURE.__class__} : {FURNITURE.dimensions()}")

abstract_factory/table_factory.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""A Factory Pattern Example
2+
The Factory Pattern Defines in Interface for creating an object
3+
and defers instantation until runtime.
4+
Used when you don't know how many or what type of objects will be needed until during runtime
5+
"""
6+
7+
from abc import ABCMeta, abstractstaticmethod
8+
9+
10+
class ITable(metaclass=ABCMeta): # pylint: disable=too-few-public-methods
11+
"""The Table Interface"""
12+
13+
@abstractstaticmethod
14+
def dimensions():
15+
"""Get the table dimensions"""
16+
17+
18+
class BigTable(ITable): # pylint: disable=too-few-public-methods
19+
"""The Big Table Concrete Class which implements the ITable interface"""
20+
21+
def __init__(self):
22+
self._height = 60
23+
self._width = 120
24+
self._depth = 80
25+
26+
def dimensions(self):
27+
return {"width": self._width, "depth": self._depth, "height": self._height}
28+
29+
30+
class MediumTable(ITable): # pylint: disable=too-few-public-methods
31+
"""The Medium Table Concrete Class which implements the ITable interface"""
32+
33+
def __init__(self):
34+
self._height = 60
35+
self._width = 110
36+
self._depth = 70
37+
38+
def dimensions(self):
39+
return {"width": self._width, "depth": self._depth, "height": self._height}
40+
41+
42+
class SmallTable(ITable): # pylint: disable=too-few-public-methods
43+
"""The Small Table Concrete Class which implements the ITable interface"""
44+
45+
def __init__(self):
46+
self._height = 60
47+
self._width = 100
48+
self._depth = 60
49+
50+
def dimensions(self):
51+
return {"width": self._width, "depth": self._depth, "height": self._height}
52+
53+
54+
class TableFactory: # pylint: disable=too-few-public-methods
55+
"""Tha Factory Class"""
56+
57+
@staticmethod
58+
def get_table(table):
59+
"""A static method to get a table"""
60+
try:
61+
if table == "BigTable":
62+
return BigTable()
63+
if table == "MediumTable":
64+
return MediumTable()
65+
if table == "SmallTable":
66+
return SmallTable()
67+
raise AssertionError("Table Not Found")
68+
except AssertionError as _e:
69+
print(_e)
70+
return None
71+
72+
73+
if __name__ == "__main__":
74+
TABLE = TableFactory().get_table("SmalTable")
75+
print(TABLE)

factory/chair_factory.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""A Factory Pattern Example
2+
The Factory Pattern is a creational pattern that defines an Interface for creating an object
3+
and defers instantiation until runtime.
4+
Used when you don't know how many or what type of objects will be needed until during runtime
5+
"""
6+
7+
from abc import ABCMeta, abstractstaticmethod
8+
9+
10+
class IChair(metaclass=ABCMeta): # pylint: disable=too-few-public-methods
11+
"""The Chair Interface"""
12+
13+
@abstractstaticmethod
14+
def dimensions():
15+
"""A static inteface method"""
16+
17+
18+
class BigChair(IChair): # pylint: disable=too-few-public-methods
19+
"""The Big Chair Concrete Class which implements the IChair interface"""
20+
21+
def __init__(self):
22+
self._height = 80
23+
self._width = 80
24+
self._depth = 80
25+
26+
def dimensions(self):
27+
return {"width": self._width, "depth": self._depth, "height": self._height}
28+
29+
30+
class MediumChair(IChair): # pylint: disable=too-few-public-methods
31+
"""The Medium Chair Concrete Class which implements the IChair interface"""
32+
33+
def __init__(self):
34+
self._height = 60
35+
self._width = 60
36+
self._depth = 60
37+
38+
def dimensions(self):
39+
return {"width": self._width, "depth": self._depth, "height": self._height}
40+
41+
42+
class SmallChair(IChair): # pylint: disable=too-few-public-methods
43+
"""The Small Chair Concrete Class which implements the IChair interface"""
44+
45+
def __init__(self):
46+
self._height = 40
47+
self._width = 40
48+
self._depth = 40
49+
50+
def dimensions(self):
51+
return {"width": self._width, "depth": self._depth, "height": self._height}
52+
53+
54+
class ChairFactory: # pylint: disable=too-few-public-methods
55+
"""Tha Factory Class"""
56+
57+
@staticmethod
58+
def get_chair(chair):
59+
"""A static method to get a table"""
60+
try:
61+
if chair == "BigChair":
62+
return BigChair()
63+
if chair == "MediumChair":
64+
return MediumChair()
65+
if chair == "SmallChair":
66+
return SmallChair()
67+
raise AssertionError("Chair Not Found")
68+
except AssertionError as _e:
69+
print(_e)
70+
return None
71+
72+
73+
if __name__ == "__main__":
74+
CHAIR_FACTORY = ChairFactory().get_chair("SmallChair")
75+
print(CHAIR_FACTORY.dimensions())

factory/classes_chair_factory.dot

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
digraph "classes_chair_factory" {
2+
charset="utf-8"
3+
rankdir=BT
4+
"0" [label="{BigChair|\l|dimensions()\l}", shape="record"];
5+
"1" [label="{ChairFactory|\l|get_chair()\l}", shape="record"];
6+
"2" [label="{IChair|\l|dimensions()\l}", shape="record"];
7+
"3" [label="{MediumChair|\l|dimensions()\l}", shape="record"];
8+
"4" [label="{SmallChair|\l|dimensions()\l}", shape="record"];
9+
"0" -> "2" [arrowhead="empty", arrowtail="none"];
10+
"3" -> "2" [arrowhead="empty", arrowtail="none"];
11+
"4" -> "2" [arrowhead="empty", arrowtail="none"];
12+
}

factory/classes_factory.dot

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
digraph "classes_chair_factory" {
2+
charset="utf-8"
3+
rankdir=BT
4+
"0" [label="{Factory|\l|get_object()\l}", shape="record"];
5+
"1" [label="{IObject|\l|dimensions()\l}", shape="record"];
6+
"2" [label="{Object1|\l|dimensions()\l}", shape="record"];
7+
"3" [label="{Object2|\l|dimensions()\l}", shape="record"];
8+
"4" [label="{Object3|\l|dimensions()\l}", shape="record"];
9+
"2" -> "1" [arrowhead="empty", arrowtail="none"];
10+
"3" -> "1" [arrowhead="empty", arrowtail="none"];
11+
"4" -> "1" [arrowhead="empty", arrowtail="none"];
12+
}

factory/factory_pattern.png

38.2 KB
Loading

factory/factory_pattern_chair.png

41.1 KB
Loading

0 commit comments

Comments
 (0)