Abstract Factory pattern of Java design patterns

Source: Internet
Author: User

The previous article (http://www.cnblogs.com/liaoweipeng/p/5768197.html) talked about the simple factory model, but the simple factory model has some problems, if you want to expand the program, the factory class must be modified, This violates the closure-open principle, so, from the design point of view, there are certain problems, how to solve? Use the abstract factory pattern to create multiple factory classes so that once you need to add new functionality, you can add new factory classes directly, without having to modify the previous code.

The concept of abstract Factory mode

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. (Baidu Encyclopedia)

UML diagram for abstract Factory mode

  

Code for Abstract Factory mode

We went on to the simple factory model to continue to explain the abstract factory model, if you need to add a non-male in the simple factory model, you need to modify the factory class generation method, although we can achieve the results we need, but contrary to the Java open-closure principle. Let's do a makeover based on the previous example.

/*** * package com.roc.factory; /** * Abstract Interface  of the product @author  */Publicinterface  Human        {publicvoid  say ();}
/*** This was factory patter package*/ Packagecom.roc.factory;/*** Man Men *@authorLIAOWP **/ Public classMansImplementsHuman {/*Say method * @see Com.roc.factory.human#say ()*/@Override Public voidsay () {System.out.println (Man); }}
/*** This was factory patter package*/ Packagecom.roc.factory;/**woman *@authorLIAOWP **/ Public classWomanImplementsHuman {/*Say method * @see Com.roc.factory.human#say ()*/@Override Public voidsay () {System.out.println (Woman); }}

The previous code is the same, now we need to create a factory class for a person, since everyone has their own factory class, it is not possible to smoke a class, yes, we will pull out a factory interface.

 Package com.roc.factory; /**  @author*/Publicinterface  Factory        {  public Human Crateman ();    }

Creating a man's factory class

 Package com.roc.factory; /**  @author*/Publicclassimplements  factory{      Public Human Crateman () {        returnnew man ();    }}

Creating a woman's factory class

 Package com.roc.factory; /**  @author*/Publicclassimplements  factory{    @Override      Public Human Crateman () {        //  TODO auto-generated method stub        return New Woman ();    }}

Client class

 Package com.roc.factory; /**  @author*/Publicclass  Client    {public  staticvoid  main (string[] args) {            Factory Factory=new  Manfactory ();        Human  man2=Factory.crateman ();        Man2.say ();            }}

Transformation completed, after my transformation, you now know how to increase the creation of non-male and female people? For such a makeover you need to add any type to add only one factory class, one product class. The client then writes out a new call. Fully conforms to the Java development and closure principles.

Application Scenarios

The first scenario is that for a product, the caller knows exactly which specific factory service to use, instantiates the specific factory, and produces the specific product.  This is the case with the iterator () method in Java collection. In the second case, just need a product, but do not want to know and do not need to know which factory for production, that is, the final choice of which specific factory decision on the producer side, they are based on the current system of the situation to instantiate a specific factory return to the user, and this decision-making process for users is transparent.

Abstract Factory pattern of Java design patterns

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.