Factory mode:
Defines an interface for creating an object, but its subclasses determine which object to instantiate. The factory method defers instantiation of an object to a subclass.
Pattern Composition:
- A group of users: The parent class defines an interface for creating objects and other common interfaces, and subclasses are responsible for creating specific implementations of object interfaces (in the case Pizzastore and Nypizzastore)
- A set of product classes: (in the case of the abstract pizza class and its specific implementation subclass)
Note:
- The subclass of each consumer is responsible for creating a (group) similar object.
UML class Diagrams:
The code specifically implements:
1, the creator of the object: Pizzastore class
//Pizza Store Parent class: Contains the abstract method of creating pizza (Createpizza)classPizzastore {//contains an abstract class of products and a simple factoryvar Pizza:pizza =Pizza ()//Provide pizza methodsfunc Orderpizza (pizzatype:pizzatype) {//get a reference to the pizza object created by the storeSelf.pizza =Createpizza (Pizzatype)//the behavior of the productPizza.prepare () pizza.bake () Pizza.cut () Pizza.box ()}//create an interface that is responsible for creating the object, but it is implemented specifically by implementing subclassesFunc Createpizza (Pizzatype:pizzatype)Pizza {returnPizza ()}}//New York Pizza Store: Responsible for creating New York-style pizzaclassNypizzastore:pizzastore {//implement the interface that is responsible for creating the object specifically OverrideFunc Createpizza (Pizzatype:pizzatype)Pizza {SwitchPizzatype { CasePizzatype.Cheese:pizza=Nycheesepizza () CasePizzatype.Clam:pizza=Nyclampizza ()}returnPizza}}//Chicago Pizza Store: Responsible for creating Chicago-style pizzaclassChicagopizzastore:pizzastore {//implement the interface that is responsible for creating the object specifically OverrideFunc Createpizza (Pizzatype:pizzatype)Pizza {SwitchPizzatype { CasePizzatype.Cheese:pizza=Chicagocheesepizza () CasePizzatype.Clam:pizza=Chicagoclampizza ()}returnPizza}}
2. Object class:
//the parent class of the objectclassPizza {var name:string=""var dough:string=""var sauce:string=""var toppings:[string]= [] //preparing the Pizza methodFunc Prepare () {println ("preparing for \ (name) ...") println ("tossing dough ...") println ("join \ (sauce) ...") forTopping:stringinchToppings {println ("\ (Topping)")}}} Func box () {println ("packing ... \ n")} func Bake () {println ("baking ...")} func cut () {println ("cropping ...") }}//a New York-flavored Cheese Pizza; it's a group of New York-flavored Clem Pizza.classNycheesepizza:pizza {Overrideinit () {super.init () Self.name="New York-style Cheese Pizza"Self.dough="Thin crust Dough"Self.sauce="Strawberry Sauce"Toppings.append ("grated Reggino Cheese") }}//a New York-flavored Clam PizzaclassNyclampizza:pizza {Overrideinit () {super.init () Self.name="New York-style Nyclam Pizza"Self.dough="Low crust Dough"Self.sauce="Cherry Sauce"Toppings.append ("grated Reggino Clam") }}//a Chicago-flavored Cheese Pizza; it's a group of Chicago-flavored Clem Pizza.classChicagocheesepizza:pizza {Overrideinit () {super.init () Self.name="Chicago-style Cheese Pizza"Self.dough="Thin crust Dough"Self.sauce="Mango sauce"Toppings.append ("grated Reggino Cheese") } //subclasses can also override methods of the parent class Overridefunc Cut () {println ("cropping: cropping solutionkeys blocky ...") }}//a Chicago-style Clam PizzaclassChicagoclampizza:pizza {Overrideinit () {super.init () Self.name="Chicago-style Clam Pizza"Self.dough="Low crust Dough"Self.sauce="grilled meat Sauce"Toppings.append ("grated Reggino Clam") } //subclasses can also override methods of the parent class Overridefunc Cut () {println ("cropping: cropping solutionkeys blocky ...") }}
3. Test code:
// first visit the New York Pizza store var pizzastore:pizzastore = Nypizzastore ()// buy a New York-flavored Cheesepizza /* The Orderpizza method of the Nypizzastore is called first, and the Createpizza method is called by the Orderpizza method, where the Orderpizza method is created by the parent class and used by the subclass, and Orderpizza does not need to know This Pizza is exactly what kind of, just want to know that this is a Pizza can prepare, cut and so on behavior is enough and Createpizza method is sub-class in accordance with the corresponding circumstances to create the corresponding flavor Pizza; Pizzastore.orderpizza (Pizzatype.cheese) // Visit the Chicago Pizza store and buy a Chicago-style ClampizzaPizzastore = chicagopizzastore () Pizzastore.orderpizza ( Pizzatype.clam)
4. Test results
Design Pattern Learning-Factory mode