Java Design Pattern-----3, Abstract Factory mode

Source: Internet
Author: User

Abstract Factory mode is the most abstract and general form of the factory pattern in all forms. Abstract Factory mode refers to a factory pattern that is used when there are multiple abstract roles. Abstract Factory mode provides the client with an interface that enables the client to create product objects in multiple product families without having to specify the product's specific circumstances.

Product family refers to the family of products that are related to function in different product hierarchy. Are generally located in the same position in different hierarchical structures. Obviously, each product family contains the number of products, and the number of product grade structure is equal, the formation of a two-dimensional coordinate system, horizontal coordinate is the product hierarchy, ordinate is the product family.  Called phase diagram. When there are multiple products of different hierarchical structure, it is necessary to use a factory method pattern to deal with the hierarchical structure of these products using multiple independent plant hierarchy structures.  If these product hierarchies are parallel, they result in multiple parallel plant hierarchy structures.  Abstract Factory mode uses the same factory hierarchy to create the product objects for these different product hierarchies. For each product family, there is a specific factory.  Each specific factory is created to belong to the same product family, but belong to different hierarchical structure of the product.  By introducing the abstract factory model, you can handle the creation of product objects in multiple product families with the same (or similar) hierarchical structure. Because each specific factory role needs to be responsible for the creation of two different hierarchical product objects, each factory role needs to provide two factory methods, respectively, for creating two hierarchical products.  Since each specific factory role needs to implement these two factory methods, it is generic and can be abstracted and moved to the abstract factory role to be declared. Like, fruit is divided into Northern Fruit: Northern Apple, Northern Banana; Southern Fruit: Southern Apple, Southern banana; Tropical Fruits: Tropical Apple, tropical banana; look, northern fruit, southern fruit, tropical fruit This is three different product families.  Here is a simple example of the abstract factory model: First determine our product family, product family for southern fruit and northern fruit, and fruit (product grade) has Apple and banana product grade, so specific products for the South Apple, northern Apple, Southern Banana, Northern banana. The code is as follows: first, each family has apples and bananas, so define two abstract classes, which contain an abstract method apple
1  Public Abstract class Implements fruit{2      Public Abstract void get (); 3 }

Banana

1  Public Abstract class Implements fruit{2      Public Abstract void get (); 3 }

The specific product of the Apple Banana is written, and each inherits the corresponding abstract class

Northern Apples

1  Public class extends Apple {2    @Override3public     void  get () {  4         System.out.println ("Harvesting Northern Apples"); 5     }67 }

Southern Apple

 Public class extends apple{    @Override    publicvoid  get () {        System.out.println ("collect Southern Apples") );    }}

Northern Banana

 Public class extends Banana {    @Override    publicvoid  get () {        System.out.println ( "Harvesting Northern Bananas");}    }

Southern Banana

1  Public class extends Banana {2    @Override3public     void  get () {  4         System.out.println ("Harvesting Southern Bananas"); 5     }6 }

The factory is then created, and each product family corresponds to a specific factory, each product family contains apples and bananas, so each plant contains apples and bananas.

Abstract Factory

1  Public Interface fruitfactory {2     // instantiate an Apple 3      Public Fruit getapple (); 4     // Instantiate a banana 5      Public Fruit Getbanana (); 6 }

North Plant
1  Public classNorthfactoryImplementsfruitfactory{2 @Override3      PublicFruit getapple () {4         return Newnorthapple ();5     }6 7 @Override8      PublicFruit Getbanana () {9         return NewNorthbanana ();Ten     } One}
Southern Factory
1  Public classSouthfactoryImplementsfruitfactory{2 @Override3      PublicFruit getapple () {4         return Newsouthapple ();5     }6 7 @Override8      PublicFruit Getbanana () {9         return NewSouthbanana ();Ten     } One}

Finally, write a run of the Main method

1  Public classMainClass {2      Public Static voidMain (string[] args) {3Fruitfactory NF =Newnorthfactory ();4         5Fruit Napple =nf.getapple ();6 napple.get ();7         8Fruit Nbanana =Nf.getbanana ();9 nbanana.get ();Ten          OneFruitfactory SF =Newsouthfactory (); A          -Fruit sapple =sf.getapple (); - sapple.get (); the          -Fruit Sbanana =Sf.getbanana (); - sbanana.get (); -     } +}

Final Run Results

Harvesting Northern Apples
Harvesting Northern Bananas
Collecting apples from the south
Collect Southern Banana

 In this case, if you want to add a product family tropical fruit, just create a new tropical product family factory, has been built in the southern and northern factories do not need to change, also comply with the open-closed principle.

But the disadvantage is also obvious, from the product level, if you want to add a product level, such as the above example only apples and bananas, if a new grape now, you need to add a grape abstract method in the abstract factory, and then implement this method in each specific factory. This is totally out of line with the open-closed principle.

   Advantages:1. It separates the specific Class 2. It makes it easy to Exchange product Series 3. It facilitates product consistency Disadvantages:Difficult to support new kinds of products the roles and responsibilities contained in the abstract factory model 1. Abstract Factory (Creator) Role: (fruitfactory)Is the core of the abstract factory pattern, which contains declarations for multiple product structures, and any factory class must implement this interface. 2, the specific factory (concrete Creator) Role: (Applefactory, Bananafactory)This is the specific factory class that implements the abstract factory interface and is responsible for instantiating the product objects in a product family. 3. Abstract Product Role: (Fruit)The parent class of an object created by the abstract factory pattern, which is responsible for describing the common interfaces common to all instances. 4. Specific products (concrete product) Role: (Apple, Banana)The concrete instance object created by the abstract schema. the method in the abstract factory corresponds to the product structure, and the specific factory corresponds to the product family.

Java Design Pattern-----3, Abstract Factory mode

Related Article

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.