-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstract_factory.py
56 lines (43 loc) · 1.54 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
class AbstractFactory:
def create_product_a(self):
raise NotImplementedError
def create_product_b(self):
raise NotImplementedError
class ConcreteFactory1(AbstractFactory):
def create_product_a(self):
return ProductA1()
def create_product_b(self):
return ProductB1()
class ConcreteFactory2(AbstractFactory):
def create_product_a(self):
return ProductA2()
def create_product_b(self):
return ProductB2()
class AbstractProductA:
def interact(self, abstract_product_b):
pass
class ProductA1(AbstractProductA):
def interact(self, abstract_product_b):
print(f"ProductA1 interacts with {abstract_product_b}")
class ProductA2(AbstractProductA):
def interact(self, abstract_product_b):
print(f"ProductA2 interacts with {abstract_product_b}")
class AbstractProductB:
def interact(self, abstract_product_a):
pass
class ProductB1(AbstractProductB):
def interact(self, abstract_product_a):
print(f"ProductB1 interacts with {abstract_product_a}")
class ProductB2(AbstractProductB):
def interact(self, abstract_product_a):
print(f"ProductB2 interacts with {abstract_product_a}")
# Example usage
if __name__ == "__main__":
factory1 = ConcreteFactory1()
product_a1 = factory1.create_product_a()
product_b1 = factory1.create_product_b()
product_a1.interact(product_b1)
factory2 = ConcreteFactory2()
product_a2 = factory2.create_product_a()
product_b2 = factory2.create_product_b()
product_a2.interact(product_b2)