Objective
A factory creates objects from a name. With a registry (a dictionary) and a decorator, each class registers itself, and the factory does not need a chain of if/elif.
Instructions
- Create a decorator
@register(name)that stores the class in a dictionaryREGISTRY, and registerDog(dog),Cat(cat) andCow(cow) with the attributesound. - Read
nand thennnames. For each one create the animal and showname: sound; if the name is not registered, showUnknown type: name.
3
dog
cow
fish
dog: woof
cow: moo
Unknown type: fish
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise