forked from onionmccabbage/beyondAdvancedPythonApril2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_factory.py
More file actions
28 lines (24 loc) · 689 Bytes
/
Copy pathmy_factory.py
File metadata and controls
28 lines (24 loc) · 689 Bytes
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
# Python provides an Absctract Base Class in abc
from abc import ABCMeta, abstractmethod
class Animal(metaclass = ABCMeta):
@abstractmethod
def do_say(self):
pass
class Dog(Animal):
def do_say(self):
print('woof')
class Cat(Animal):
def do_say(self):
print('meaow')
class Bat(Animal):
def do_say(self):
print('------')
# declare a factory method
class CreatureFactory():
def make_sound(self, object_type):
return eval(object_type)().do_say()
if __name__ == '__main__':
cf = CreatureFactory()
# ask the user which creature
animal = input('which creature? ')
cf.make_sound(animal)