From: http://blog.csdn.net/ericzhong83/article/details/7604728 factory method Mode (Factory pattern):
Defines an interface to create an object, but subclasses decide which of the classes to instantiate, that is, to create objects from subclasses.
Principle:
To rely on abstraction, do not rely on specific classes.
Case:
First explain what the factory is:
If you open a pizza shop (Pizzastore abstract class) sell various flavors of pizza (pizza subclass), then you need to prepare the corresponding pizza (create pizza object) according to customer requirements, and then bake, slice, packaging;
The simplest approach is to create the corresponding pizza object in Pizzastore based on the customer's requirements (type judgment), and then invoke the bake, slice, and wrap method of pizza itself (implemented by the pizza Abstract class);
But this kind of code lacks elasticity, because you let an abstract class to rely on the concrete object; We can create a factory to produce pizza, returning different pizza objects based on the different types of values passed in, that is, moving the code that creates the object from Pizzastore to the factory. But it's just a programming trick, not a pattern.
In the factory method pattern, we define an abstract interface (Create_pizza) as an abstract factory in Pizzastore, and Order_pizza is its customer, and the creation of the pizza object is placed in the Pizzastore subclass to resolve.
The existing cheese and clam two pizza, as well as the NY and Chicago two outlets, each with a different taste of the same pizza – improved to cater to local tastes, with major differences from different raw materials, So we implemented four pizza types (Nystylecheesepizza, Nystyleclampizza, Chicagostylecheesepizza, and Chicagostyleclampizza), Each uses a different mix of raw materials, according to the customer's city and choose the style we create different objects; According to the factory method, we put the object-created code into the Pizzastore subclass to implement.
Code:
#!/usr/bin/pythonclassPizza:name=""Dough=""sauce=""Toppings= [] defPrepare (self):Print "Preparing%s"%Self.namePrint "Dough:%s"%Self.doughPrint "Sauce:%s"%Self.saucePrint "Add Toppings:" forNinchself.toppings:Print "%s"%NdefBake (self):Print "Bake for minutes at." defCut (self):Print "cutting into diagonal slices." defbox (self):Print "Put into official box." defget_name (self):returnSelf.nameclassPizzastore:defOrder_pizza (Self, pizza_type): Self.pizza=Self.create_pizza (Pizza_type) Self.pizza.prepare () Self.pizza.bake () self.pizza.cut () sel F.pizza.box ()returnSelf.pizzadefCreate_pizza (Self, pizza_type):PassclassNystylecheesepizza (Pizza):def __init__(self): Self.name="NY Style Cheese Pizza"Self.dough="NY Dough"Self.sauce="NY Sauce"Self.toppings.append ("NY toopping A") Self.toppings.append ("NY toopping B")classChicagostylecheesepizza (Pizza):def __init__(self): Self.name="Chicago Style Cheese Pizza"Self.dough="Chicago Dough"Self.sauce="Chicago Sauce"Sefl.toppings.append ("Chicago toopping A") defCut (self):Print "cutting into square slices."classNystyleclampizza (Pizza):def __init__(self): Self.name="NY Style Clam Pizza"Self.dough="NY Dough"Self.sauce="NY Sauce"Self.toppings.append ("NY toopping A") Self.toppings.append ("NY toopping B")classChicagostyleclampizza (Pizza):def __init__(self): Self.name="Chicago Style Clam Pizza"Self.dough="Chicago Dough"Self.sauce="Chicago Sauce"Self.toppings.append ("Chicago toopping A") defCut (self):Print "cutting into square slices."classNypizzastore (pizzastore):defCreate_pizza (Self, pizza_type):ifPizza_type = ="Cheese": returnNystylecheesepizza ()elifPizza_type = ="Clam": returnNystyleclampizza ()Else: returnNoneclassChicagopizzastore (pizzastore):defCreate_pizza (Self, pizza_type):ifPizza_type = ="Cheese": returnChicagostylecheesepizza ()elifPizza_type = ="Clam": returnChicagostyleclampizza ()Else: returnNoneif __name__=="__main__": Ny_store=Nypizzastore () Chicago_store=Chicagopizzastore () Pizza= Ny_store.order_pizza ("Cheese") Print "Mike ordered a%s."%Pizza.get_name ()PrintPizza= Chicago_store.order_pizza ("Clam") Print "John ordered a%s."%Pizza.get_name ()Print
Output:
Preparing ny Style Cheese Pizza dough:ny dough sauce:ny sauce Add toppings: ny toopping A for-minutes at. Cutting into diagonal slices. Put into official box. Mike ordered a NY style Cheese pizza.preparing Chicago Style Clam Pizza dough:chicago dough Sauce:chicago sauce< C10/>add toppings: ny toopping A ny toopping B for + minutes at . Cutting into square slices. Put into official box. John ordered a Chicago Style Clam Pizza.
Design mode: Factory method Mode (Python)