Created
February 11, 2019 10:46
-
-
Save lalabuy948/99d99abe9def500862eb88ea263aa49e to your computer and use it in GitHub Desktop.
Simple factory patter in Python3
This file contains 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 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