forked from Sean-Bradley/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory_concept.py
63 lines (43 loc) · 1.38 KB
/
factory_concept.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
# pylint: disable=too-few-public-methods
# pylint: disable=arguments-differ
"The Factory Concept"
from abc import ABCMeta, abstractmethod
class IProduct(metaclass=ABCMeta):
"A Hypothetical Class Interface (Product)"
@staticmethod
@abstractmethod
def create_object():
"An abstract interface method"
class ConcreteProductA(IProduct):
"A Concrete Class that implements the IProduct interface"
def __init__(self):
self.name = "ConcreteProductA"
def create_object(self):
return self
class ConcreteProductB(IProduct):
"A Concrete Class that implements the IProduct interface"
def __init__(self):
self.name = "ConcreteProductB"
def create_object(self):
return self
class ConcreteProductC(IProduct):
"A Concrete Class that implements the IProduct interface"
def __init__(self):
self.name = "ConcreteProductC"
def create_object(self):
return self
class Creator:
"The Factory Class"
@staticmethod
def create_object(some_property):
"A static method to get a concrete product"
if some_property == 'a':
return ConcreteProductA()
if some_property == 'b':
return ConcreteProductB()
if some_property == 'c':
return ConcreteProductC()
return None
# The Client
PRODUCT = Creator.create_object('b')
print(PRODUCT.name)