Java Design pattern Learning record-Abstract Factory mode

Source: Internet
Author: User

Objective

The previous blog introduced the Simple factory model and the factory method pattern, this time introducing the abstract factory pattern, the difference between the abstract factory pattern and the factory method pattern is the complexity of the objects that need to be created.

Abstract Factory mode

The abstract factory model is built around a super factory to create other factories. This super factory is called a factory in other factories, mainly to solve the problem of interface selection.

To illustrate:

Or continue to the use of the user hand of the example, now the production of mobile phone factory found that different mobile phone use of accessories are not the same, the need for separate production accessories. For example, Iphonx uses the original deep camera, as well as the OLED display, while the Iphone8 uses a new sensor consisting of a camera, as well as a Retina HD display. This requires the accessory factory of each accessory to provide the appropriate accessories. In order to take a simple name when writing code instances, for the time being, the Iphonex uses a camera and a monitor, while the Iphone8 uses a B-camera and a B-sensor.

The code example is as follows:

Display

/***/Publicinterface  Display {}
 /**   * Iphonex display  */ public  class  adisplay implements   Display { public  Span style= "color: #000000;"    > Adisplay () {System.out.println ( "##### manufacturing OLED display #####" 
/***/Publicclassimplements  Display    {public  bdisplay () {        System.out.println ("##### manufacturing HD Retina display #####");}    }

Camera

/***/Publicinterface  Camera {}
 /**   * Iphonex camera  */ public  class  Acamera implements   Camera { public   Acamera () {System.out.println ( "##### manufacturing original deeply webcam #####" 
/***/Publicclassimplements  camera{    public  Bcamera () {        System.out.println ("#####  manufacturing camera ##### with new photosensitive components");}    }

Factory class

/*** Abstract Factory class*/ Public Abstract classAbstractfactory {/*** Get the display *@return     */     Public AbstractDisplay Getdisplay (); /*** Get a webcam *@return     */     Public AbstractCamera Getcamera ();}
/*** Iphonex Parts factory class*/ Public classIphonexpartsfactoryextendsabstractfactory{/*** Get the display * *@return     */@Override PublicDisplay Getdisplay () {return NewAdisplay (); }    /*** Get webcam * *@return     */@Override PublicCamera Getcamera () {return NewAcamera (); }}
/*** Iphone8 Parts factory class*/ Public classIphone8partsfactoryextendsabstractfactory{/*** Get the display * *@return     */@Override PublicDisplay Getdisplay () {return NewBdisplay (); }    /*** Get webcam * *@return     */@Override PublicCamera Getcamera () {return NewBcamera (); }}

User class

/*** User Class*/ Public classCustomer {/*** Manufacturing Accessories *@paramaf*/     Public Static voidMadeiphone (Abstractfactory af) {//accessories for making iphoneAf.getdisplay ();    Af.getcamera (); }     Public Static voidMain (string[] args) {abstractfactory iphonepartsfactory=Newiphonexpartsfactory (); //Accessories for Manufacturing IphonexMadeiphone (iphonepartsfactory); Iphonepartsfactory=Newiphone8partsfactory (); //Accessories for Manufacturing IPhone8Madeiphone (iphonepartsfactory); }}

Operation Result:

##### Manufacturing OLED display ########## manufacturing original deep camera ########## manufacturing HD Retina display ##########  manufacturing with new photosensitive components of the camera #####

Factory mode is also part of the creation model, which is a further extension of the factory method model. When a subsystem requires some product objects, and these products belong to more than one product hierarchy, then in order to consume these product objects of responsibility and the responsibility of the gold these product objects to separate, this time can use the abstract Factory mode. This way, the party using the product does not directly participate in the creation of the product, but only requests the required product to a common factory interface.

The relationship class diagram for the factory pattern is as follows:

The roles in the abstract factory pattern are the same as the roles in the factory method pattern, which is not covered here and can be viewed as a description of the role of a factory method pattern.

Advantages and disadvantages of abstract Factory mode advantages: separating interfaces and implementations

The client uses an abstract factory to create the desired object, and the client simply does not know who the specific implementation is, and the client is only programming for Trump's interface. In other words, the client is decoupled from the specific product implementation.

Make it easy to switch product families

Because a specific factory implementation represents a product family, such as the above example from the Iphonex accessories to the Iphone8 accessories only need to switch the specific factory.

Disadvantages:

The downside of the abstract factory model is that it is not easy to extend new products. If you need to add a new product to the entire product family, you need to modify the abstract factory for a long time, which will result in modifying all of the factory implementation classes.

Java Design pattern Learning record-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.