Description
If you want to assemble a computer, the motherboard, CPU, memory and other components in accordance with a stable combination of steps, the basic process is unchanged, and the composition of the various parts of the computer can be different performance, different price, or different version, When composing a computer, you only need to select different components to create different configurations of the computer according to the basic same process. That is, the intent of the builder pattern is to separate the construction process of a complex object from the presentation, which uses the same build steps to act on different sub-objects to construct "complex objects" of different representations.
Instance:
Build a fast food ordering system from the point of order
classBurger ():"""staple food category, price name"""name="" Price=0.0defGetPrice (self):returnSelf.pricedefSetprice (self,price): Self.price= PricedefGetName (self):returnSelf.nameclassCheeseburger (Burger):"""Cheese Burger""" def __init__(self): Self.name="Cheese Burger"Self.price=10.0classSpicychickenburger (Burger):"""Spicy Chicken Burger""" def __init__(self): Self.name="Spicy Chicken Burger"Self.price=15.0classSnack ():"""Snacks , price and name"""name="" Price= 0.0type="SNACK" defGetPrice (self):returnSelf.pricedefSetprice (Self, Price): Self.price= PricedefGetName (self):returnSelf.nameclasschips (Snack):"""French Fries""" def __init__(self): Self.name="Chips"Self.price= 6.0classchickenwings (Snack):"""Chicken Wings""" def __init__(self): Self.name="Chicken Wings"Self.price= 12.0classBeverage ():"""Beverages"""name="" Price= 0.0type="Beverage" defGetPrice (self):returnSelf.pricedefSetprice (Self, Price): Self.price= PricedefGetName (self):returnSelf.nameclassCoke (beverage):"""Cola""" def __init__(self): Self.name="Coke"Self.price= 4.0classmilk (beverage):"""Milk""" def __init__(self): Self.name="Milk"Self.price= 5.0classorder ():"""Order object, an order containing a staple food, a snack, a drink"""Burger=""Snack=""Beverage="" def __init__(self,orderbuilder): Self.burger=Orderbuilder.bburger Self.snack=Orderbuilder.bsnack self.beverage=Orderbuilder.bbeveragedefShow (self):Print("burger:%s"%self.burger.getName ())Print("snack:%s"%self.snack.getName ())Print("beverage:%s"%self.beverage.getName ())#built byclassOrderbuilder ():"""The Orderbuilder is the so-called "builder" in the builder's model, separating the construction of the order from the representation to achieve the purpose of decoupling. In the construction process of the above order, if the order is defined directly through the parameters (its construction and the expression is not separated), at the same time in the order generation in many places, the need to modify the order content, you need to change everywhere, business risk has increased a lot. """Bburger=""Bsnack=""Bbeverage="" defAddburger (Self,xburger): Self.bburger=XburgerdefAddsnack (self,xsnack): Self.bsnack=Xsnackdefaddbeverage (self,xbeverage): Self.bbeverage=XbeveragedefBuild (self):returnorder (self)#Director Classclassorderdirector ():"""in the builder mode, you can also add a Director class to arrange the construction steps of an existing module. This class can be of great use when there are more stringent order requirements in the builder. """Order_builder="" def __init__(self,order_builder): Self.order_builder=Order_builderdefCreateorder (self,burger,snack,beverage): Self.order_builder.addBurger (Burger) Self.order_builder.addSna CK (snack) self.order_builder.addBeverage (beverage)returnSelf.order_builder.build ()#Scenario Implementationif __name__=="__main__": Order_builder=Orderbuilder () Order_builder.addburger (Spicychickenburger ()) Order_builder.addsnack (Chips ()) Order_builder.ad Dbeverage (Milk ()) order_1=Order_builder.build () order_1.show ( )
Execution Result:
Burger:spicy Chicken Burger
Snack:chips
Beverage:milk
Advantages:
So that the internal appearance of the product can be independently changed, so that customers do not need to know the internal components of the product details. More granular control over the build process, separating the build code from the presentation code.
Disadvantages:
It is difficult to cope with the demand change of the "step-by-build algorithm".
Applicability:
1. The products that need to be generated have complex internal structure
2. The properties of the product objects that need to be generated depend on each other, and the builder mode can break the generation sequence.
Reference Link: https://yq.aliyun.com/articles/70416?spm=a2c4e.11155435.0.0.495838daVpgS2g
python-Builder Mode