We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d3ad23e commit 59436eaCopy full SHA for 59436ea
patterns/creational/abstract_factory.py
patterns/creational/factory.py
@@ -0,0 +1,34 @@
1
+class Enemy():
2
+ def attack(self):
3
+ print("Attacking wildly with {0}!".format(self.weapon))
4
+
5
6
+class Orc(Enemy):
7
+ def __init__(self):
8
+ self.weapon = "greataxe"
9
10
11
+class Skeleton(Enemy):
12
13
+ self.weapon = "greatsword"
14
15
16
+class Dragon(Enemy):
17
18
+ self.weapon = "fire breathing"
19
20
21
+def factory(enemy_type):
22
+ if (enemy_type == Orc):
23
+ return Orc()
24
+ elif (enemy_type == Skeleton):
25
+ return Skeleton()
26
+ elif (enemy_type == Dragon):
27
+ return Dragon()
28
+ else:
29
+ raise TypeError
30
31
32
+if __name__ == '__main__':
33
+ d = factory(Dragon)
34
+ d.attack()
patterns/creational/maze.py
0 commit comments