[Original · tutorial · serialization] "android big talk Design Pattern"-Design Pattern creation pattern Chapter 3: abstract factory pattern

Source: Internet
Author: User
<Big talk design model> description and copyright notice of this tutorial

L this document references and uses free images and content on the Internet, and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

L if you have any questions or suggestions about this document, go to the official blog

Http://www.cnblogs.com/guoshiandroid/ (with the following contact information), we will carefully refer to your suggestions and modify this document as needed to benefit more developers!

L The latest and complete content of "big talk design mode" will be regularly updated on the official blog of guoshi studio. Please visit the blog of guoshi studio.

Http://www.cnblogs.com/guoshiandroid/get more updates.

Guoshi studio is a technical team dedicated to enterprise-level application development on the Android platform. It is committed to the best Android Application in China.ProgramDevelopment institutions provide the best Android enterprise application development training services.

Official contact information for Enterprise Training and Development Cooperation:

Tel: 18610086859

Email: hiheartfirst@gmail.com

QQ: 1740415547

QQ: 148325348

Guoshi studio is better for you!



 

  View other parts:Overall description and Chapter index of this tutorial

PDF download link

MM's birthday

Example of 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 Factory ModelUMLFigure: 

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 Application Scenario Analysis andCodeImplementation: 

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:

package COM. diermeng. designpattern. abstractfactory. impl;

Import COM. diermeng. designpattern. abstractfactory. hamburg;

/*

* Center hamburg at the McDonald's branch south of the commercial street

*/

Public class southmacdonaldhamburg extends Hamburg {

/*

* Get Hamburg

* @ see COM. diermeng. designpattern. abstractfactory. hamburg # Get ()

*/

Public void get () {

system. out . println ("Get the hamburger of McDonald's branch south of central commercial street ");

}

}

Create a McDonald's hamburger at Hualian:

package COM. diermeng. designpattern. abstractfactory. impl;

Import COM. diermeng. designpattern. abstractfactory. hamburg;

/*

* Hualian hamburg at the McDonald's branch over there

*/

Public class hualianmacdonaldhamburg extends Hamburg {

/*

* Get Hamburg

* @ see COM. diermeng. designpattern. abstractfactory. hamburg # Get ()

*/

Public void get () {

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 ClassHualianmacdonaldchickenwingExtendsChickenwing {

/*

* 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.

Note: This document references and uses free and open images and content on the Internet and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used.Benefit.

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.