@@ -0,0 +1,2322 @@
+#%0A# Python Design Patterns: Builder%0A# Author: Jakub Vojvoda %5Bgithub.com/JakubVojvoda%5D%0A# 2016%0A#%0A# Source code is licensed under MIT License%0A# (for more details see LICENSE)%0A# %0A%0Aimport sys%0A%0A#%0A# Product%0A# the final object that will be created using Builder%0A#%0Aclass Product:%0A  def __init__(self):%0A    self._partA = %22%22%0A    self._partB = %22%22%0A    self._partC = %22%22%0A%0A  def makeA(self, part):%0A    self._partA = part%0A        %0A  def makeB(self, part):%0A    self._partB = part%0A  %0A  def makeC(self, part):%0A    self._partC = part%0A  %0A  def get(self):%0A    return self._partA +%22 %22+ self._partB +%22 %22+ self._partC  %0A%0A#%0A# Builder%0A# abstract interface for creating products%0A#%0Aclass Builder:%0A  def __init__(self):%0A    self._product = Product()%0A    %0A  def get(self):%0A    return self._product%0A%0A  def buildPartA():%0A    pass%0A    %0A  def buildPartB():%0A    pass%0A  %0A  def buildPartC():%0A    pass%0A%0A#%0A# Concrete Builders%0A# create real products and stores them in the composite structure%0A#%0Aclass ConcreteBuilderX(Builder):%0A  def __init__(self):%0A    Builder.__init__(self)%0A%0A  def buildPartA(self):%0A    self._product.makeA(%22A-X%22)%0A  %0A  def buildPartB(self):%0A    self._product.makeB(%22B-X%22)%0A  %0A  def buildPartC(self):%0A    self._product.makeC(%22C-X%22)    %0A%0Aclass ConcreteBuilderY(Builder):%0A  def __init__(self):%0A    Builder.__init__(self)%0A  %0A  def buildPartA(self):%0A    self._product.makeA(%22A-Y%22)%0A  %0A  def buildPartB(self):%0A    self._product.makeB(%22B-Y%22)%0A  %0A  def buildPartC(self):%0A    self._product.makeC(%22C-Y%22)    %0A%0A#%0A# Director%0A# responsible for managing the correct sequence of object creation%0A#%0Aclass Director:%0A  def __init__(self):%0A    self._builder = None%0A  %0A  def set(self, builder):%0A    self._builder = builder  %0A%0A  def get(self):%0A    return self._builder.get()%0A  %0A  def construct(self):%0A    self._builder.buildPartA()%0A    self._builder.buildPartB()%0A    self._builder.buildPartC()%0A      %0A%0Aif __name__ == %22__main__%22:%0A  builderX = ConcreteBuilderX()%0A  builderY = ConcreteBuilderY()  %0A  %0A  director = Director()%0A  director.set(builderX)  %0A  director.construct()%0A  %0A  product1 = director.get()%0A  print(%221st product parts: %22 + product1.get())%0A  %0A  director.set(builderY)  %0A  director.construct()%0A  %0A  product2 = director.get()%0A  print(%222nd product parts: %22 + product2.get())%0A                                                                                        

add Builder pattern