Python-style abstract Factory mode

Source: Internet
Author: User

Abstract Factory mode:

Provides an interface for users to create multiple related or dependent objects without specifying a specific class.

Principle:

Relies on abstractions and does not rely on specific classes.

Instance:

Make different flavors of pizza with different raw materials, create a factory with different raw materials, and make different flavors of pizza in various physical stores. Create an abstract type (pizzaingredientfactory) for a product family (dough, Sauce, cheese, and clam), Subclasses of this type (Nypizzaingredientfactory and Chicagopizzaingredientfactory) define the method by which the product is produced.

The difference between Factory mode and abstract Factory mode:

The factory pattern is to define a factory's abstract interface in a derived class, and then the base class is responsible for creating the concrete object; The abstract factory pattern is to maintain a product family, defined by the base class the method that the product is produced, and the customer develops according to the interface of the derived class.

Code:

#!/usr/bin/python#-*-coding:utf-8-*-import sysreload (SYS) sys.setdefaultencoding (' utf-8 ') ' Pizza ' class Pizza:name        = "" dough = none sauce = None Cheese = None Clam = None def prepare (self): Pass Def Bake (self): Print "Bake 25 minutes at 350. ". Decode (' Utf-8 ') def cut (self): print" cut into diagonal slices. ". Decode (' Utf-8 ') def box (self): print" is placed in the official box. ". Decode (' Utf-8 ') def get_name (self): return self.name def set_name (self, name): Self.name = name D EF to_string (self): string = "%s:\n"% self.name string + = "dough:%s\n"% self.dough.to_string () if self. Dough Else "string + =" sauce:%s\n "% self.sauce.to_string () if self.sauce else" "string + =" Cheese:%s\ N "% self.cheese.to_string () if Self.cheese else" "string + =" clam:%s\n "% self.clam.to_string () if Self.clam E LSE "return string" ' What category of pizza ' class Cheesepizza (Pizza): Def __init__ (self, ingredient_factory): self.i NgredIent_factory = Ingredient_factory def prepare (self): print "Ready:%s"% self.name Self.dough = Self.ingredi Ent_factory.create_dough () Self.sauce = Self.ingredient_factory.create_sauce () Self.cheese = Self.ingredient _factory.create_cheese () class Clampizza (Pizza): Def __init__ (self, ingredient_factory): self.ingredient_factory = Ingredient_factory def prepare (self): print "Ready:%s"% self.name Self.dough = self.ingredient_factory.c Reate_dough () Self.sauce = Self.ingredient_factory.create_sauce () Self.clam = Self.ingredient_factory.create        _clam () "Pizza shop" class Pizzastore:def Order_pizza (Self, pizza_type): Self.pizza = Self.create_pizza (Pizza_type) Self.pizza.prepare () Self.pizza.bake () Self.pizza.cut () Self.pizza.box () return Self.pi Zza def create_pizza (self, pizza_type): Pass ' New York Pizza entity store 1 ' class Nypizzastore (Pizzastore): def create_pizza (SE        LF, Pizza_type):Ingredient_factory = Nypizzaingredientfactory () if Pizza_type = = "Cheese": Pizza = Cheesepizza (Ingredien t_factory) Pizza.set_name ("New York style Cheese pizza". Decode (' Utf-8 ')) elif Pizza_type = = "Clam": Pizza = Clam        Pizza (ingredient_factory) pizza.set_name ("New York style Clam Pizza". Decode (' Utf-8 ')) Else:pizza = None Return pizza ' Chicago Pizza store 2 ' class Chicagopizzastore (Pizzastore): Def create_pizza (self, pizza_type): Ingredien T_factory = Chicagopizzaingredientfactory () if Pizza_type = = "Cheese": Pizza = Cheesepizza (ingredient_fa ctory) pizza.set_name ("Chicago Style Cheese Pizza". Decode (' Utf-8 ')) elif Pizza_type = = "Clam": Pizza = Clampiz        Za (ingredient_factory) pizza.set_name ("Chicago Style clam Pizza". Decode (' Utf-8 ')) Else:pizza = None        Return pizza ' production of Pizza Factory ' class Pizzaingredientfactory:def Create_dough (self): Pass def create_sauce (self): Pass DEF Create_cheese (self): Pass def Create_clam (self): Pass "the entity factory producing Pizza 1" Class Nypizzaingredientfactory (pizzaingred Ientfactory): def create_dough (self): return Thindough () def create_sauce (self): return Marinarasauce ( ) def Create_cheese (self): return Freshcheese () def create_clam (self): return Freshclam () "Production of pizza entity factory 2 ' Class Chicagopizzaingredientfactory (pizzaingredientfactory): def create_dough (self): return Thickdough () d EF Create_sauce (self): return Mushroomsauce () def Create_cheese (self): return Bluecheese () def Create_ Clam (self): return Frozenclam () class Dough:def to_string (self): Passclass Thindough (Dough): def to_st Ring (self): return "thin Dough" class thickdough (Dough): def to_string (self): return "thick dough" class sauce:def To_string (self): Passclass marinarasauce (Sauce): def to_string (self): return "tomato Sauce" class mushroomsauce (Sau CE): def to_string (SELF): return "mushroom Sauce" class cheese:def to_string (self): Passclass Freshcheese (Cheese): def to_string (self) : return "fresh Cheese" class Bluecheese (Cheese): def to_string (self): return "blue Cheese" class Clam:def to_string ( Self): Passclass Freshclam (Clam): def to_string (self): return "fresh Clam" class Frozenclam (Clam): Def to_st Ring (self): return "Frozen clam" if __name__ = = "__main__": # created two pizza stores ny_store = Nypizzastore () Chicago_store = Ch Icagopizzastore () # ordered a cheese-flavored pizza in the first pizza object pizza = Ny_store.order_pizza ("cheese") print pizza.to_string () p    Rint "Mike ordered a%s"% Pizza.get_name () Print pizza = Chicago_store.order_pizza ("clam") print pizza.to_string () Print "John ordered a%s"% Pizza.get_name () print

Results:

Preparation: New York style Cheese Pizza Bake 25 minutes at 350. Cut into diagonal slices. Put it in the official box. New York style Cheese Pizza:    dough: thin dough    sauce: tomato sauce    cheese: Fresh cheese Mike ordered a New York style cheese Pizza ready: Chicago style clam Pizza baked for 25 minutes at 350. Cut into diagonal slices. Put it in the official box. Chicago Style Clam Pizza:    dough: thick dough    sauce: clam with mushroom sauce:    frozen clam John ordered a Chicago style clam pizza.

Python-style abstract Factory mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.