Skip to content

Instantly share code, notes, and snippets.

@takahashilabo
Last active May 19, 2022 11:11
Show Gist options
  • Save takahashilabo/9a07ea5a48c6f1b139b9ef6a444ed9fd to your computer and use it in GitHub Desktop.
Save takahashilabo/9a07ea5a48c6f1b139b9ef6a444ed9fd to your computer and use it in GitHub Desktop.
Composite Pattern
class Entry:
def add():
pass
def getName():
pass
class File_(Entry):
def __init__(self, name):
self.__name = name
def add(self):
pass
def getName(self):
return self.__name
def print(self):
print(self.getName())
class Directory(Entry):
def __init__(self, name):
self.__directory = []
self.__name = name
def add(self, e):
self.__directory.append(e)
def getName(self):
return self.__name
def print(self):
print(self.getName())
for e in self.__directory:
e.print()
d = Directory("d1")
d.add(File_("f1"))
d.add(File_("f2"))
d.print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment