Abstract Factory mode is used in Android

Source: Internet
Author: User

Abstract Factory mode (Factory)

Abstract Factory mode is the creation mode of objects, which is the result of further extension and expansion of the factory method pattern. Abstract factory models are more abstract and more general in nature.

We know that the factory method pattern is generated for a single product hierarchy, and the abstract factory model here is for multiple product hierarchies. Of course, a single product hierarchy also applies to abstract factory patterns. The following is a description of its specific hierarchy chart:

From the above structure, you can see that the abstract factory pattern involves multiple hierarchical product roles, and for each product hierarchy, there is a dedicated factory role to create the corresponding product, which is the abstract product role. The following are related role descriptions:

A, abstract product roles A, B, C ... : This role is a multiple hierarchy of products and is the parent of the corresponding product role.

b, specific product roles A, B, C ...: This role is the specific product role that corresponds to the abstract product above, and the relationship between the abstract product role and the specific product role in each product structure is a one-to-many relationship.

C, Abstract Factory role: This role is an abstract factory role, and is the parent of all specific factory roles that create product roles. This role is the core method class in the pattern, which declares how to create different hierarchical structure products.

D, specific factory roles A, B, C ... : This role is a factory role that specifically creates a product hierarchy, a many-to-one relationship to the abstract factory role, and a role to create direct contact with the specific product.

We know the principle of the abstract factory model, then the following is a direct example of its use, after all, it is correct to use it to explain the true understanding of its principle.

The example is this, in the product module there are two product hierarchy (of course can be multiple), respectively, for the package and entertainment. In each type of product structure, there should be a category of target questions, such as: online packages and sharing entertainment (this has occurred in the actual product).

OK, here's my specific code:

Abstract product Role (package):

Public abstract class Meal {

Stringmealname;

int Mealno;

float Mealprice;

Public String Getmealname () {

return Mealname;

}

Public void setmealname (String mealname) {

this. mealname = mealname;

}

Public int Getmealno () {

return Mealno;

}

Public void setmealno (int mealno) {

this. Mealno = Mealno;

}

Public float getmealprice () {

return Mealprice;

}

Public void setmealprice (float mealprice) {

this. Mealprice = Mealprice;

}

@Override

Public String toString () {

return "meal[mealname=" + mealname + ", mealno=" + Mealno

+", mealprice=" + mealprice + "]";

}

}

Specific product roles (General package):

Public class Commonmeal extends Meal {

Public Commonmeal () {

Setmealname (" normal Internet Package");

Setmealno (1);

Setmealprice (20.0f);

}

}

Specific product roles (high-speed package):

Public class Speedmeal extends Meal {

Public Speedmeal () {

Setmealname (" high-Speed Internet package");

Setmealno (2);

Setmealprice (30.0f);

}

}

Abstract product roles (sharing entertainment):

Public abstract class Entertainment {

Stringetname;

int Etno;

StringEttype;

Public String Getetname () {

return Etname;

}

Public void setetname (String etname) {

this. etname = etname;

}

Public int Getetno () {

return Etno;

}

Public void setetno (int etno) {

this. Etno = Etno;

}

Public String Getettype () {

return Ettype;

}

Public void setettype (String ettype) {

this. Ettype = Ettype;

}

@Override

Public String toString () {

return "entertainment[etname=" + etname + ", etno=" + Etno

+", ettype=" + ettype + "]";

}

}

Specific product roles (film):

Public class Movie extends Entertainment {

Public Movie () {

Setetname (" film");

Setetno (1);

Setettype (" video");

}

}

Specific product roles (fiction):

Public class Books extends Entertainment {

Public Books () {

Setetname (" novel");

Setetno (2);

Setettype (" reading");

}

}

Abstract factory roles (packages and entertainment):

Public interface productfactory {

/* The method of the network meal * /

Public Meal mealfactory ();

/ * The method of entertainment * /

Public Entertainment etfactory ();

}

Specific factory role (factory a):

Public class Productfactoryone implements productfactory {

@Override

Public Meal Mealfactory () {

return new Speedmeal ();

}

@Override

Public Entertainment Etfactory () {

return new Books ();

}

}

Specific factory role (Factory B):

Public class Productfactorytwo implements productfactory {

@Override

Public Meal Mealfactory () {

return new Commonmeal ();

}

@Override

Public Entertainment Etfactory () {

return new Movie ();

}

}

The specific calling code and the factory method pattern are similar, here no longer repeat, my code runs:

Here, we have analyzed and used the abstract factory model.

Technology Exchange Group:179914858

Abstract Factory mode is used in Android

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.