You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Separates the construction of a complex object from its representation so that the same construction process can create different representations.
classDiagram
direction LR
Builder --o Director
ConcreteBuilder --|> Builder
Product <.. ConcreteBuilder : builds
class Director {
+Construct()
}
class Builder{
+BuildPart()
}
class ConcreteBuilder{
+BuildPart()
}
class Product{
}
"""Builder Concept Sample Codehttps://sbcode.net/python/builder/#builderbuilder_conceptpy"""fromabcimportABCMeta, abstractmethodclassIBuilder(metaclass=ABCMeta):
"The Builder Interface"@staticmethod@abstractmethoddefbuild_part_a():
"Build part a"@staticmethod@abstractmethoddefbuild_part_b():
"Build part b"@staticmethod@abstractmethoddefbuild_part_c():
"Build part c"@staticmethod@abstractmethoddefget_result():
"Return the final product"classBuilder(IBuilder):
"The Concrete Builder."def__init__(self):
self.product=Product()
defbuild_part_a(self):
self.product.parts.append('a')
returnselfdefbuild_part_b(self):
self.product.parts.append('b')
returnselfdefbuild_part_c(self):
self.product.parts.append('c')
returnselfdefget_result(self):
returnself.productclassProduct():
"The Product"def__init__(self):
self.parts= []
classDirector:
"The Director, building a complex representation."@staticmethoddefconstruct():
"Constructs and returns the final product"returnBuilder()\
.build_part_a()\
.build_part_b()\
.build_part_c()\
.get_result()
# The ClientPRODUCT=Director.construct()
print(PRODUCT.parts)