0 takeaway

Factory method model people are created, but they are all the same type, lack of love, hatred, happiness, sadness and joy and other emotions, human life is too dull, forget to define human gender, how to do that? It is also impossible to build everything from scratch, so we want to recreate people under the existing conditions and make use of the old things as much as possible. How should race (Product category) be transformed? How do you get people to love and hate? Define mutually exclusive genders, and then plant a seed in each individual: opposites attract, and when they mature, they are bound to find one. From a design perspective, a specific object can be identified by two coordinates: skin color and gender


Skin color and gender coordinates

  • After analyzing the product category, how to transform the production factory category (bagua furnace)?

    There is only one production facility, which will either produce all men or all women.

    The present has some production equipment – the eight diagrams furnace apart, so nuwa will use the “eight diagrams copy”, the original eight diagrams furnace a change two, and slightly modified, became a female eight diagrams furnace (only female race) and male eight diagrams furnace (only male race), so began to prepare for production

    Reproducing humans


    One interface, multiple abstract classes, and then N implementation classes, each race is an abstract class, and gender is implemented in each implementation class

    In particular, it needs to be explainedHumanFactoryIn this interface, three methods are defined to produce three people of different skin color, which is the Y coordinate of our coordinate graph. Its two implementation classes are gender, which is the X coordinate of the coordinate graph

    A produced object is uniquely identified by X coordinates (gender) and Y coordinates (skin color)

  • The Human interface is like code


  • There are three abstract classes of race. The abstract attributes of the principal species define skin color and language: white race, black race, and yellow race




Each abstract class has two implementation classes that implement the most detailed and concrete things common: skin color and the language-specific implementation class that implements skin color and gender definitions

  • Take the yellow female race for example


  • Male yellow race


All that’s left is how do you make a human

  • Interface HumanFactory



    In the interface, it can be seen that the eight trigrams furnaces can produce people of different colors. How many eight trigrams furnaces are there? Two, respectively produce female and male, female and male gossip stove




    With the race and the eight triglespot, let’s recreate the scene of making people



    Men and women of all colors are produced Back to think about our design, don’t know if you have been to the factory, each factory many workshop, every workshop, production lines, production of different products We can put the gossip furnace metaphor for the workshop, the gossip of furnace production process (production white, black or yellow) as production line, So, is a female production workshop, specializing in the production of all kinds of women, one is male production workshop, specializing in the production of a variety of color of skin to men Under such a design, each workshop and every line of duty is very clear, all manufactured products in the workplace can have coupling relationship, do you want to know the world black, yellow, white is the ratio of: 1∶4∶6, then this needs to do a good proportion in the firing distribution, in a workshop to coordinate well this is the abstract factory model

1 definition

  • Official definitions Provide an interface for creating families of related or dependent objects without specifying their concrete classes.




    Generic class diagrams for abstract factory patterns


    The abstract factory pattern is a upgrade version of the factory method pattern In multiple varieties, business classification, object generated by the abstract factory pattern need is a very good solution Let’s take a look at the abstract factory general source code, first of all, there are two influence each other’s product line (product family), such as making cars on the left side of the door and the door on the right side, The two should be equal in number — the constraint between the two objects, each model of car door is different, is the constraint of the product hierarchy structure, let’s first look at the class diagram of the two product families


    Generic source class diagram for abstract factory patterns


    Note the corresponding circles and boxes on the class diagram. Two abstract product classes can have relationships, such as common inheritance or implementing an abstract class or interface

  • Abstract product class


  • The implementation class for product A1


  • The implementation class for product A2



    Product B is similar and will not be repeated

  • Abstract Factory Class AbstractCreator is responsible for defining the functionality to be implemented by each factory. In common code, the Abstract factory class defines product creation for two product families


    With N product families, there should be N creation methods in the abstract factory class

How do you create a product that is done by a concrete implementation class

  • Implementation classes for product level 1


  • Product level 2 implementation classes


There should be M implementation factory classes for M product classes, and in each implementation factory, the production tasks of different product families are implemented

How do you generate an implementation-independent object in a specific business?



In the scenario class, there is no method related to the implementation class. For a product, we only need to know its factory method to directly produce a product object, and do not care about its implementation class

2 Application Scenarios

A family of objects (or a group of unrelated objects) all have the same constraints

For example, a text editor and a picture processor are both software entities, but the text editor under * NIx and the text editor under Windows have the same function and interface, but the code implementation is different, and the picture processor has a similar situation. There is a common constraint: the operating system type. We can then use the abstract factory pattern to generate editors and image processors for different operating systems

Three advantages

encapsulation

The implementation class of each product is not the concern of a high-level module what is it? It’s an interface, it’s an abstraction, it doesn’t care how the object is created, who’s responsible for that? Factory class, as long as I know who the factory class is, I can create an object that I need, save time and effort, which is what good design is all about

Constraints within a product family are in a closed state

For example, when it comes to the ratio of males to females in childbirth, we must have our own plan. We must not allow women to flourish and men to decline, otherwise the advantages of women will not be reflected. In the abstract factory pattern, should have such a constraint: every production of a woman, will produce 1.2 men at the same time, the production process for calls to the top of the factory class module is transparent, it does not need to know this constraint, I just want a yellow women products, specific product family within the constraints are implemented in the factory

4 faults

Product family scaling is very difficult

In the case of common code, if we were to add a product C, that is to say, the product family was increased from two to three, just look at how much our program changed! AbstractCreator adds a method createProductC(), and then both implementation classes need to be modified. This is a serious violation of the open close principle, and we’ve been saying that abstract classes and interfaces are contract change contracts. It’s called “toxic code” — anything associated with this code can be dangerous!

It is the product family that is difficult to scale, not the product level. In this model, the product level is very easy to scale. That is to say, horizontal scaling is easy, vertical scaling is difficult. In humans, for example, there are only two genders in the product hierarchy, male and female, and one gender in the real world: Bisexual men, that we want to extend the product level is also very easy, and increase the three products, corresponding to different color of skin, and then create a factory class, specifically responsible for different color of skin people bisexual people create a task, complete by extending to achieve change in the demand, from this point, the abstract factory pattern is in accordance with the open closed principle

Product grade structure and product family



Abstract factories are better than factory methods when a factory can create all objects in a product family that belong to different product hierarchies!!

Five practice coding




Java classes related





Python related









6 Source code application


MySQL db connection, for a product family Mybatis factory application



The default implementation



Initialize the transaction factory by taking environment variables from the configuration, assign values to them, get the transaction instance object from the transaction factory, get the execution thread pool using it as a parameter, and then regenerate it to return the default SQLSession instance

7 Best Practices

Abstract Factory mode is a simple mode, using a lot of scenarios, everyone in the software product development process, involving different operating systems, can consider using abstract factory mode, for example, an application needs to run on three different platforms, how would you design? Design three different applications? No, use abstract factory mode to mask the influence of operating system on application. The software functions, application logic, and UI on the three different operating systems should be very similar, with the only difference being that different factory methods are called and different product classes handle the information interacting with the operating system