Skip to content

Instantly share code, notes, and snippets.

@jasper-lyons
Created January 22, 2014 13:26
Show Gist options
  • Save jasper-lyons/8558672 to your computer and use it in GitHub Desktop.
Save jasper-lyons/8558672 to your computer and use it in GitHub Desktop.
Creational Design Patterns in Python: The Builder
#### 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()
@jasper-lyons
Copy link
Author

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.

painter.set_pen(new_pen)

using middleware

app = require(express);
app.use(express.basicAuth('username', 'password'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment