Created
May 19, 2022 11:08
-
-
Save takahashilabo/93c5886a6eead21939596756c1ffa99e to your computer and use it in GitHub Desktop.
Mario and Kuribo on Factroy Method Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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