Skip to content

Commit 59436ea

Browse files
author
Joshua Goller
committed
implements factory pattern
1 parent d3ad23e commit 59436ea

File tree

3 files changed

+34
-84
lines changed

3 files changed

+34
-84
lines changed

patterns/creational/abstract_factory.py

Whitespace-only changes.

patterns/creational/factory.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
def __init__(self):
13+
self.weapon = "greatsword"
14+
15+
16+
class Dragon(Enemy):
17+
def __init__(self):
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

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)