This pattern is to construct a hierarchical hierarchy of final objects.
In the software release process, different java,python,node. Js,zip Compressed Package,
You can use a different stage to use the builder mode.
fromEnumImportEnumImporttimepizzaprogress= Enum ('pizzaprogress','Queued Preparation Baking ready') Pizzadough= Enum ('Pizzadough','thin Thick') Pizzasauce= Enum ('pizzatopping','Tomato Creme_fraiche') pizzatopping= Enum ('pizzatopping','mozzaralla double_mozzarella bacon ham mushrooms red_onion oregano') Step_delay= 3classPizza:def __init__(self, name): Self.name=name Self.dough=None self.sauce=None self.topping= [] def __str__(self):returnSelf.namedefPrepare_dough (self, Dough): Self.dough=DoughPrint('preparing the {} dough of your {} ...'. Format (Self.dough.name, self)) time.sleep (Step_delay)Print('Done with the {} dough'. Format (self.dough.name))classMargaritabuilder:def __init__(self): Self.pizza= Pizza ('Margarita') self.progress=pizzaprogress.queued Self.baking_time= 5defPrepare_dough (self): self.progress=pizzaprogress.preparation Self.pizza.prepare_dough (Pizzadough.thin)defadd_sauce (self):Print('adding the tomato sauce to your margarita ...') Self.pizza.sauce=Pizzasauce.tomato time.sleep (step_delay)Print('Done with the tomato sauce') defadd_topping (self):Print('adding the topping (double mozzarella, oregano) to your Margarita') Self.pizza.topping.append ([I forIinch(Pizzatopping.double_mozzarella, Pizzatopping.oregano)]) Time.sleep (Step_delay)Print('Done with the topping (double Mozzarrella, oregano)') defBake (self): self.progress=pizzaprogress.bakingPrint('Baking your margarita for {} seconds'. Format (self.baking_time)) Time.sleep (self.baking_time) self.progress=Pizzaprogress.readyPrint('your Margarita is ready')classCreamybaconbuilder:def __init__(self): Self.pizza= Pizza ('Creamy Bacon') self.progress=pizzaprogress.queued Self.baking_time= 7defPrepare_dough (self): self.progress=pizzaprogress.preparation Self.pizza.prepare_dough (Pizzadough.thick)defadd_sauce (self):Print('adding the creme fraiche sauce to you creamy bacon') Self.pizza.sauce=Pizzasauce.creme_fraiche time.sleep (step_delay)Print('Done with the creme fraiche sauce') defadd_topping (self):Print('adding the topping (Mozzaralla, bacon, ham, mushrooms, red onion, oregano) to you creamy bacon') self.pizza.topping.append ([t forTinch(Pizzatopping.mozzaralla, Pizzatopping.bacon, Pi Zzatopping.ham, Pizzatopping.mushrooms, Pizzatopping.red_onion, Pizzatopping.oregano)]) Time.sleep (Step_delay)Print('Done with the topping (mozzarella, bacon, ham, mushrooms, red onion, oregano)') defBake (self): self.progress=pizzaprogress.bakingPrint('Baking your creamy bacon for {} seconds'. Format (self.baking_time)) Time.sleep (self.baking_time) self.progress=Pizzaprogress.readyPrint('Your creamy bacon is ready')classWaiter:def __init__(self): Self.buider=NonedefConstruct_pizza (self, builder): Self.builder=Builder [Step () forStepinch(Builder.prepare_dough, Builder.add_sauce, builder.add_topping, Builder.bake)] @prope RtydefPizza (self):returnSelf.builder.pizzadefValidate_style (Builders):Try: Pizza_style= Input ('What Pizza would do like, [M]argarita or [C]reamy bacon,') Builder=Builders[pizza_style] () valid_input=TrueexceptKeyerror as err:Print('Sorry, only Margarita (key m) and creamy bacon (key c) is available') return(False, None)return(True, builder)defMain (): Builders= Dict (M=margaritabuilder, c=creamybaconbuilder) Valid_input=False while notValid_input:valid_input, builder=Validate_style (Builders)Print() Waiter=Waiter () Waiter.construct_pizza (builder) Pizza=Waiter.pizzaPrint() Print('Enjoy your {}!'. Format (pizza))if __name__=='__main__': Main ()
The builder model of learning Python design patterns