Created
January 19, 2014 05:07
-
-
Save atsuya046/8500672 to your computer and use it in GitHub Desktop.
GoF design pattern with Python
This file contains hidden or 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
# -*- coding:utf-8 -*- | |
""" | |
@author: Diogenes Augusto Fernandes Herminio <[email protected]> | |
https://gist.github.com/420905#file_builder_python.py | |
""" | |
class Director(object): | |
def __init__(self): | |
self.buider = None | |
def construct_building(self): | |
self.builder.new_building() | |
self.builder.build_floor() | |
self.builder.build_size() | |
def get_building(self): | |
return self.builder.building | |
# Abstract Builder | |
class Builder(object): | |
def __init__(self): | |
self.building = None | |
def new_building(self): | |
self.building = Building() | |
# Concrete Builder | |
class BuilderHouse(Builder): | |
def build_floor(self): | |
self.building.floor = 'One' | |
def build_size(self): | |
self.building.size = 'Big' | |
class BuilderFlat(Builder): | |
def build_floor(self): | |
self.building.floor = 'More than One' | |
def build_size(self): | |
self.building.size = 'Small' | |
# Product | |
class Building(object): | |
def __init__(self): | |
self.floor = None | |
self.size = None | |
def __repr__(self): | |
return 'Floor: {0.floor} | Size: {0.size}'.format(self) | |
# Client | |
if __name__ == "__main__": | |
director = Director() | |
director.builder = BuilderHouse() | |
director.construct_building() | |
building = director.get_building() | |
print(building) | |
director.builder = BuilderFlat() | |
director.construct_building() | |
building = director.get_building() | |
print(building) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey you have a Typo at line 10 (buider is builder for sure) and for your Abstract builder use ABCMeta as Metaclass like this