Skip to content

Instantly share code, notes, and snippets.

@jbiason
Last active June 13, 2018 19:46
Show Gist options
  • Save jbiason/7fc075402409e0e3b8b8e4a751914806 to your computer and use it in GitHub Desktop.
Save jbiason/7fc075402409e0e3b8b8e4a751914806 to your computer and use it in GitHub Desktop.
class Base:
version = "unknown"
code = "unknown"
@classmethod
def identifier(cls):
return cls.code + '/' + cls.version
def do(self):
print("I'm a stupid method in the base class")
class SmartV1(Base):
version = 'v1'
code = 'smart'
def do(self):
print("I'm a terribly named method in the smart v1 class")
print("I identify myself as " + self.identifier())
class DumbV2(Base):
version = 'v2'
code = 'dumb'
def do(self):
print("Also terriblee noumed mefot, but I R smat")
print("There people call I " + self.identifier())
class Factory:
"""Because classes, that's why."""
available_results = [SmartV1, DumbV2]
@classmethod
def create_code(cls, code):
for klass in cls.available_results:
if klass.identifier() == code:
return klass()
return None
def main():
"""This is just an example."""
obj = Factory.create_code('smart/v1')
obj.do()
obj = Factory.create_code('dumb/v2')
obj.do()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment