Skip to content

Instantly share code, notes, and snippets.

@lalabuy948
Created February 11, 2019 10:46
Show Gist options
  • Save lalabuy948/99d99abe9def500862eb88ea263aa49e to your computer and use it in GitHub Desktop.
Save lalabuy948/99d99abe9def500862eb88ea263aa49e to your computer and use it in GitHub Desktop.
Simple factory patter in Python3
class Button(object):
html = ""
def get_html(self):
return self.html
class Image(Button):
html = "<img></img>"
class Input(Button):
html = "<input></input>"
class Flash(Button):
html = "<obj></obj>"
class ButtonFactory():
def create_button(self, typ):
target_class = typ.capitalize()
return globals()[target_class]()
button_obj = ButtonFactory()
button = ['image', 'input', 'flash']
for b in button:
print(button_obj.create_button(b).get_html())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment