Created
January 22, 2014 13:26
-
-
Save jasper-lyons/8558672 to your computer and use it in GitHub Desktop.
Creational Design Patterns in Python: The Builder
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
#### The product to contruct #### | |
class Product(object): | |
def __init__(self, extra): | |
self.extra = extra | |
def do_somthing(self): | |
print "Doing Something{0}!".format(self.extra) | |
#### The Builder for a type of product #### | |
class NormalBuilder(object): | |
def build_product(self): | |
return Product("") | |
#### The Builder for another type of product #### | |
class ExtraBuilder(object): | |
def build_product(self): | |
return Product(" Else") | |
#### The class using the builders to build and serve products #### | |
class Director(object): | |
def set_builder(self, builder): | |
self.builder = builder | |
def get_product(self): | |
return self.builder.build_product() | |
def main(): | |
director = Director() | |
director.set_builder(ExtraBuilder) | |
product = director.get_product() | |
product.do_somthing() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This may not seem at first like a particularly useful pattern, especially to new programmers yet it is exactly the sort of pattern used when drawing ui's.
using middleware