Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save takahashilabo/93c5886a6eead21939596756c1ffa99e to your computer and use it in GitHub Desktop.
Save takahashilabo/93c5886a6eead21939596756c1ffa99e to your computer and use it in GitHub Desktop.
Mario and Kuribo on Factroy Method Pattern
class Chara:
def __init__(self):
self.__cs = []
def add(self, c):
self.__cs.append(c)
def draw(self) :
pass
def drawAll(self):
for c in self.__cs:
c.draw()
class Mario(Chara):
def draw(self):
print("マリオ")
class Kuribo(Chara):
def draw(self):
print("クリボ")
class Factory:
def create(self):
return self.createChara()
def createChara(self):
pass
class MarioFactory(Factory):
def createChara(self):
return Mario()
class KuriboFactory(Factory):
def createChara(self):
return Kuribo()
chara = Chara()
chara.add(MarioFactory().create())
chara.add(KuriboFactory().create())
chara.drawAll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment