forked from gennad/Design-Patterns-in-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
55 lines (43 loc) · 1.5 KB
/
template.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
class AbstractGame:
"""An abstract class that is common to several games in which
players play against the others, but only one is playing at a
given time.
"""
def __init__(self, *args, **kwargs):
if self.__class__ is AbstractGame:
raise TypeError('abstract class cannot be instantiated')
def playOneGame(self, playersCount):
self.playersCount = playersCount
self.initializeGame()
j = 0
while not self.endOfGame():
self.makePlay(j)
j = (j + 1) % self.playersCount
self.printWinner()
def initializeGame(self):
raise TypeError('abstract method must be overridden')
def endOfGame(self):
raise TypeError('abstract method must be overridden')
def makePlay(self, player_num):
raise TypeError('abstract method must be overridden')
def printWinner(self):
raise TypeError('abstract method must be overridden')
# Now to create concrete (non-abstract) games, you subclass AbstractGame
# and override the abstract methods.
class Chess(AbstractGame):
def initializeGame(self):
# Put the pieces on the board.
pass
def makePlay(player):
# Process a turn for the player
pass
# --------- Alex's Martelli example ---------
class AbstractBase(object):
def orgMethod(self):
self.doThis()
self.doThat()
class Concrete(AbstractBase):
def doThis(self):
pass
def doThat(self):
pass