-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryMethod.py
More file actions
33 lines (28 loc) · 1.05 KB
/
FactoryMethod.py
File metadata and controls
33 lines (28 loc) · 1.05 KB
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
###############
# FactoryMethod
# Factory Method define an interface for creating an object, but let subclass to decide which class to instantiate.in other words, Factory Method defer instantiation to its subclass.
###############
#Product -> define the interface of objects the factory method create
class Product:
def action(self):
pass
#ConcreteProduct -> implements the Product interface
class ConcreteProduct(Product):
def action(self):
print(self.__class__.__name__)
#Creator -> declare the factory method, which may has its default implementation
class Creator:
def factoryMethod(self):
#also can has its default implementation
pass
def operation(self):
self.obj = self.factoryMethod()
self.obj.action()
#ConcreteCreator -> overrides the factory method to return a concreate product object
class ConcreteCreator(Creator):
def factoryMethod(self):
return ConcreteProduct()
#main
if __name__ == "__main__":
creator = ConcreteCreator()
creator.operation()