-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_factory.py
70 lines (49 loc) · 1.47 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
class Dog:
"""Dog class """
def __init__(self, name, age):
self._name = name
self._age = age
def speak(self):
return 'Av Av'
def __str__(self):
return 'Dog'
class Cat:
"""Cat class"""
def __init__(self, name, age):
self._name = name
self._age = age
def speak(self):
return 'Mjau'
def __str__(self):
return 'Cat'
class DogFactory:
"""Concrete factory """
def get_pet(self):
"""Returns dog object"""
return Dog('Laki', 2)
def get_food(self):
"""Returns dogs food object"""
return 'Dog food'
class CatFactory:
"""Concrete factory """
def get_pet(self):
"""Returns dog object"""
return Cat('Micika', 3)
def get_food(self):
"""Returns dogs food object"""
return 'Cat food'
class PetStore:
def __init__(self, pet_factory=None):
"""pet_factory is Abstract factory"""
self._pet_factory = pet_factory
def get_pet(self):
return self._pet_factory.get_pet()
def get_pet_food(self):
return self._pet_factory.get_food()
def show_pet(self):
"""Utility to display details of returned object by concrete factory"""
pet = self._pet_factory.get_pet()
pet_food = self._pet_factory.get_food()
print("Our pet is '{}'".format(pet))
print("Our pet says '{}'".format(pet.speak()))
print("Our pet eats '{}'".format(pet_food))