Summary of HeadFirst Design Patterns_Chapter 4 Factory Patterns

Summary after reading: ( mainly refer to P160+P161 )

1. Dependency Inversion Principle: P142 (Abstract ideological design, interface-oriented programming, extension-oriented rather than modification-oriented.)

    A variable cannot hold a reference to a specific class; (the base class uses new, that is, holds a reference to a specific class, and uses the factory method to transfer the new specific class to the subclass, that is, the line is .. = new NYPizzaStore();)

    Do not let the class derive from concrete classes; (if you derive from concrete classes, you will depend on concrete classes, please derive from abstract classes or interfaces, i.e., pizzaStore nyPizzaStore =)

    Do not override the implemented method in the base class; (the implemented method in the base class should be applicable to all derived classes as far as possible)

2. The similarities and differences between the factory method P160 and the abstract method P161 : P158 (In order to realize the decoupling of the pizza shop from the specific types of pizza, that is, the abstract base class of the pizza shop will not be modified in the future, only multiple new subclasses are needed to overload createPizza () method can be)

    Factory method: The instantiation of the concrete pizza class is placed in the pizza shop subclass, and the pizza shop can directly use the abstract type. The process is as follows:

       (1) Create a new pizza store pizzaStore nyPizzaStore = new NYPizzaStore();

       (2) Place an order nyPizzaStore.orderPizza("chess"); (where orderPizza has been implemented in the base class, orderPizza does not know the specific pizza type)

       (3) Making pizza Pizza pizza = creatPizza("chess"); will return new NYStyleChessPizza() in it

       (4) The final production process pizza.prepare(); etc.

    Abstract factory: create a product family abstract class, eg: raw material factory abstract class _ including product families such as create cheese(), creat ham(), creat salt(), etc.; while the factory method only creates a product abstract class, eg: pizza shop abstract class_includes products such as createpizza();

       (1) Create a new pizza store pizzaStore nyPizzaStore = new NYPizzaStore();

       (2) Place an order nyPizzaStore.orderPizza("chess"); (where orderPizza has been implemented in the base class, orderPizza does not know the specific pizza type)

       (3) Make pizza Pizza pizza = creatPizza("chess"); in which it will return new ChessPizza("nyIngredientFactory")

       (4) The final production process pizza.prepare(); etc. After having the abstract factory, rewrite the original prepare() method, (Formally begin the part of the abstract method: ) In which cheese dough = ingredientFactory.creatDough(); other ingredients are similar. In a specific raw material factory, such as return new TinCrustDough();

Subsequent implementation code subsequent editing. . . . . .


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325914524&siteId=291194637