Example of an abstract factory Model Application Scenario:
Sweet Time flies, GG and mm live a romantic life of a fairy tale prince and princess. Seeing MM's birthday approaching, GG is in a hurry. After all, this is my first girlfriend's first birthday. After thinking about tens of millions of methods, I asked a lot of friends around me. This silly GG is still not sure how to do it ~~~~ (>_< )~~~~
Ah! Love, always think too much to do too little ^_^
It's almost midnight, and GG is still querying Google and Baidu about how to give his sweatheart a birthday. At this point, the text message suddenly rang and opened it. It said, "Dear, I know that you have been thinking about how we have a birthday these days. In fact, everything is very simple. It's easy .", After reading the text message, GG suddenly experienced a surging body of warm traffic and felt so happy ^ _ ^. There is a mm who understands people so well! I was about to reply to the text message, and the cell phone rang again. I wrote: "We are still going to McDonald's. But this time we are going to change to another place and go to McDonald's in Hualian." ^_^ ", GG was moved to the text message and was speechless. The text message replied: "The Life of all old women is from:-o ". Gg and mm are both immersed in sweetness and happiness. ^_^
Abstract Factory mode explanation:
Abstract factory pattern is the most abstract and general factory pattern in all forms. Abstract Factory mode can provide an interface to the client so that the client can create product objects for multiple product families without specifying the specific product type.
Abstract The methods in the factory correspond to the product structure, and the specific factory corresponds to the product family.
Provide an interface forcreating families of related or dependent objects without specifying theirconcrete classes.
Abstract The UML diagram of the factory model:
Abstract The roles in the factory model and their responsibilities are as follows:
Abstract Factory role: it is the core of the abstract factory model, including the declaration of multiple product structures. Any factory class must implement this interface.
Concrete Creator: A factory class is an implementation of an abstract factory. It is responsible for instantiating product objects in a product family.
Abstract Product role: the parent class of all objects created in the abstract mode. It describes the common interfaces of all instances.
Product-specific product role: the instance object created in the abstract mode.
In-depth analysis of abstract factory models:
Abstract Factory mode is a creation design mode when a product has multiple abstract roles.
In accordance with the Li's replacement principle, child classes are also applicable where the parent class is applicable. In the actual system, what we need is the Instance Object of the subclass of the same type as the parent class, instead of the parent class itself, which is the instance of the specific subclass of these abstract products. A specific factory class is used to create instances of concrete sub-classes of abstract products.
When each abstract product has more than one concrete subclass, how does the factory role determine which subclass to instantiate? For example, there are two abstract product roles, and each abstract Product role has two specific products. The abstract factory model provides two factory roles, which correspond to the two product roles respectively. Each factory role is only responsible for the instantiation of one product role. Each factory class is only responsible for creating instances of a specific subclass of an abstract product.
Each mode is a solution to a certain problem. The factory method mode is for a product level structure, while the abstract factory mode is for multiple product level structures.
What is a product family? A product family is a family of products with different product levels and functions. Generally, it is located at the same position in different levels. Obviously, each product family contains the number of products, which is equal to the number of product level structures to form a two-dimensional coordinate system. The horizontal coordinate is the product level structure, and the vertical coordinate is the product family.
Each product family has a specific factory. Each specific factory creation belongs to the same product family, but is classified into products of different levels.
By introducing the abstract factory model, you can create product objects in multiple product families with the same (or similar) level structure.
Each factory role is responsible for creating product objects of different levels. Therefore, each factory role must provide a number of factory methods, products used to create the corresponding number of levels.
As shown in:
Abstract Factory mode scenario analysis and code implementation:
Mm is going to go to McDonald's for his birthday, but this request is to go to McDonald's in Hualian, which is a different place. We need to change the taste and mood. This is a good embodiment of the abstract factory model. First, for different McDonald's stores, each product, for example, a hamburger, is a hamburger. However, while a hamburger in each place follows the unified standard, it tries its best to highlight its own characteristics, in this way, customers can be better attracted and retained, because people's preferences and tastes vary in different places, but no matter how different they are, they will always be hamburgers, has basic functions of Hamburg. At the same time, each branch has a series of products, such as Hamburg and chicken wings, which constitute a hierarchical structure of the product.
In short: McDonald's headquarters is equivalent to abstract factories. Each branch is equivalent to a specific factory, and each product is different. In this way, the characteristics of each branch are different while the uniformity is maintained, which is suitable for attracting and retaining customers in different environments.
The UML Model diagram is as follows:
The specific implementation code is as follows:
Create a new interface for food:
PackageCom. diermeng. designpattern. abstractfactory; /* * Interfaces for all foods */ Public interfaceFood { /* * How to get food */ Public voidGet (); } |
Interface for creating a McDonald's store:
PackageCom. diermeng. designpattern. abstractfactory; /* * McDonald's primary store */ Public interfaceFoodfactory { // Instantiate the hamburger PublicFood gethamburg (); // Instantiate chicken wings PublicFood getchickenwing (); } |
Create an abstract base class for Hamburg
PackageCom. diermeng. designpattern. abstractfactory; /* * Hamburger abstract parent class */ Public abstract classHamburgImplementsFood { /* * How to get a hamburger */ Public abstract voidGet (); } |
Create an abstract base class for chicken wings
PackageCom. diermeng. designpattern. abstractfactory; /* * Chicken wings abstract class */ Public abstract classChickenwingImplementsFood { /* * How to obtain chicken wings */ Public abstract voidGet (); } |
Create a McDonald's branch in the south of central commercial street
PackageCom. diermeng. designpattern. abstractfactory. impl; ImportCom. diermeng. designpattern. abstractfactory. Food; ImportCom. diermeng. designpattern. abstractfactory. foodfactory; /* * McDonald's branch in the south of central commercial street */ Public classSouthmacdonaldImplementsFoodfactory { /* * Get Hamburg * @ See COM. diermeng. designpattern. abstractfactory. foodfactory # gethamburg () */ PublicFood gethamburg (){ Return newSouthmacdonaldhamburg (); } /* * Get chicken wings * @ See COM. diermeng. designpattern. abstractfactory. foodfactory # getchickenwing () */ PublicFood getchickenwing (){ Return newSouthmacdonaldchickenwing (); } } |
Establish a McDonald's branch in Hualian
PackageCom. diermeng. designpattern. abstractfactory. impl; ImportCom. diermeng. designpattern. abstractfactory. Food; ImportCom. diermeng. designpattern. abstractfactory. foodfactory; /* * McDonald's Hualian Branch */ Public classHualianmacdonaldImplementsFoodfactory { /* * Get Hamburg * @ See COM. diermeng. designpattern. abstractfactory. foodfactory # gethamburg () */ PublicFood gethamburg (){ Return newHualianmacdonaldhamburg (); } /* * Get chicken wings * @ See COM. diermeng. designpattern. abstractfactory. foodfactory # getchickenwing () */ PublicFood getchickenwing (){ Return newHualianmacdonaldchickenwing (); } } |
Create a hamburger at McDonald's south of the central commercial street:
PackageCom. diermeng. designpattern. abstractfactory. impl; ImportCom. diermeng. designpattern. abstractfactory. Hamburg; /* * Hamburg at McDonald's branch south of central commercial street */ Public classSouthmacdonaldhamburgExtendsHamburg { /* * Get Hamburg * @ See COM. diermeng. designpattern. abstractfactory. Hamburg # Get () */ Public voidGet (){ System.Out. Println ("Get the hamburger of McDonald's branch south of central commercial street "); } } |
Create a McDonald's hamburger at Hualian:
PackageCom. diermeng. designpattern. abstractfactory. impl; ImportCom. diermeng. designpattern. abstractfactory. Hamburg; /* * Hamburg at the McDonald's branch in Hualian */ Public classHualianmacdonaldhamburgExtendsHamburg { /* * Get Hamburg * @ See COM. diermeng. designpattern. abstractfactory. Hamburg # Get () */ Public voidGet (){ System.Out. Println ("Get the hamburger of the McDonald's branch in Hualian "); } } |
Build McDonald's chicken wings south of central commercial street
PackageCom. diermeng. designpattern. abstractfactory. impl; ImportCom. diermeng. designpattern. abstractfactory. chickenwing; /* * Chicken wings at McDonald's branch south of central commercial street */ Public classSouthmacdonaldchickenwingExtendsChickenwing { /* * Get chicken wings * @ See COM. diermeng. designpattern. abstractfactory. chickenwing # Get () */ Public voidGet (){ System.Out. Println ("Get chicken wings from McDonald's branch south of central commercial street "); } } |
Build the chicken wings of McDonald's in Hualian
PackageCom. diermeng. designpattern. abstractfactory. impl; ImportCom. diermeng. designpattern. abstractfactory. chickenwing; /* * Chicken wings at the McDonald's branch in Hualian */ Public classHualianmacdonaldchickenwing ExtendsChickenwing { /* * Get chicken wings * @ See COM. diermeng. designpattern. abstractfactory. chickenwing # Get () */ Public voidGet (){ System.Out. Println ("getting chicken wings from McDonald's branch in Hualian "); } } |
Finally, we create a test client:
PackageCom. diermeng. designpattern. abstractfactory. client; ImportCom. diermeng. designpattern. abstractfactory. Food; ImportCom. diermeng. designpattern. abstractfactory. foodfactory; ImportCom. diermeng. designpattern. abstractfactory. impl. hualianmacdonald; ImportCom. diermeng. designpattern. abstractfactory. impl. southmacdonald; /* * Test the client */ Public classAbstractfactorytest { Public static voidMain (string [] ARGs ){ // Declare and instantiate the McDonald's branch on the south of the central commercial street Foodfactory southmacdonald =NewSouthmacdonald (); // Obtain the hamburger of the McDonald's branch south of the central commercial street Food southmacdonaldhamburg = southmacdonald. gethamburg (); Southmacdonaldhamburg. Get (); // Obtain the chicken wings of the McDonald's branch south of the central commercial street Food southmacdonaldchickenwing = southmacdonald. getchickenwing (); Southmacdonaldchickenwing. Get (); // Declare and instantiate the McDonald's branch in Hualian Foodfactory hualianmacdonald =NewHualianmacdonald (); // Obtain the hamburger of the McDonald's branch in Hualian Food hualianmacdonaldhamburg = hualianmacdonald. gethamburg (); Hualianmacdonaldhamburg. Get (); // Obtain the chicken wings of the McDonald's branch in Hualian Food hualianmacdonaldchickenwing = hualianmacdonald. getchickenwing (); Hualianmacdonaldchickenwing. Get (); } } |
The output result is as follows:
Get Hamburg from McDonald's branch south of central commercial street Get chicken wings from McDonald's branch south of central commercial street Get the hamburger from the McDonald's branch in Hualian Get chicken wings from the McDonald's branch in Hualian |
Advantages and disadvantages of the abstract factory model:
Advantage: the client is no longer responsible for the specific creation of objects, but is responsible for handing over this responsibility to a specific factory class. The client is responsible for calling objects. When a product of the product family is involved in a factory class, it is very friendly to the client. More importantly, if you want to change to another product family, all you have to do is add the corresponding product family members and add a specific product factory.
Disadvantage: when a new product is added, that is, when the product structure changes, the abstract factory class design should be modified, this leads to the need to modify all the specific factory classes, resulting in an increase in objective workload.
Brief Introduction to the practical application of the abstract factory model:
Abstract Factory mode is designed for the creation of multiple product series, which has great guiding significance in the design of the persistence layer. Because of the cross-platform nature of Java, the persistence layer should generally consider database problems, such as MySQL and Oracle. Each database is equivalent to a product series, the persistence layer must design the common interfaces of different product generations to facilitate user operations on the database and facilitate database migration. The well-known hibernate draws on the design method of the abstract factory model.
Tip:
Abstract Factory models consider the creation of different product series and are not available everywhere. In addition, when new products are added, the design of the abstract factory needs to be changed, which will lead to a great deal of work. Therefore, we must consider the product structure at the beginning of the planning and strive to reduce the possibility of participating in the product, the abstract factory is relatively stable.