Java design pattern-Abstract Factory mode

Source: Internet
Author: User

abstract Factory pattern is a creative design pattern designed to create an abstract factory, which is then produced by a concrete factory that is instantiated.

when to use: System products have more than one product family, and the system only consumes the products of one of the families.

Application Example: work, in order to participate in some parties, there must be two sets or more sets of clothes, such as business attire (complete set, a series of specific products), fashion (set, a series of specific products), and even for a family, there may be business women, business men, fashion women, fashion men, These are all complete sets, namely a series of specific products. Suppose a situation (in reality does not exist, otherwise, will not be able to enter communism, but to illustrate the abstract Factory model), in your home, a certain wardrobe (specific factory) can only store one such clothes (set, a series of specific products), each time to take this set of clothes is also naturally removed from this wardrobe. With OO thought to understand, all the wardrobe (specific factory) is a wardrobe class (abstract factory) one, and each set of clothes also includes a specific jacket (a specific product), trousers (a specific product), these specific tops are actually tops (abstract products), The specific trousers are also trousers (another abstract product).

Pros: When multiple objects in a product family are designed to work together, it ensures that the client always uses only objects from the same product family.

Disadvantage: Product family expansion is very difficult, to add a series of a product, both in the abstract Creator Riga code, but also in the concrete inside add code.

use the scene: 1, QQ for skin, a whole set of change together. 2. Generate programs for different operating systems.

Note: product family is difficult to expand, product level is easy to expand.

code example:

First, class diagram:

Second, the code:

1. Create a color System product interface

 Public Interface Color {  publicvoid  Draw ();}

2. Create a color system specific product

 Public class Implements Color {    @Override    publicvoid  Draw () {                System.out.println ("is red" );    }}
 Public class Implements Color {    @Override    publicvoid  Draw () {        System.out.println ( "is blue");}    }

3. Create a shape-system product interface

 Public Interface Shape {  publicvoid  Draw ();}

4. Create a shape-specific product

 Public class Implements Shape {    @Override    publicvoid  Draw () {        System.out.println ( "pattern is rectangle");}    }
 Public class Implements Shape {    @Override    publicvoid  Draw () {        System.out.println ("pattern is circular" );    }}

5. Create an abstract factory to transform to a specific factory

 Public Abstract class abstractfactory {   publicabstract  Shape getshape (String shaptype);        Public Abstract Color GetColor (String colortype);}

6. Create a specific color factory and shape factory, all inherited from the abstract factory

 Public classColor_factoryextendsabstractfactory {@Override PublicShape Getshape (String shaptype) {return NULL; } @Override PublicColor GetColor (String colortype) {if(ColorType = =NULL) {            return NULL; }        if(Colortype.equalsignorecase ("Red")){            return NewRed (); }        if(Colortype.equalsignorecase ("Blue")){            return NewBlue (); }        return NULL; }}
 Public classShape_factoryextendsabstractfactory {@Override PublicShape Getshape (String shaptype) {if(Shaptype = =NULL){            return NULL; }        if(Shaptype.equalsignorecase ("Circle")){            return NewCircle (); }        if(Shaptype.equalsignorecase ("Rantage")){            return NewRantage (); }        return NULL; } @Override PublicColor GetColor (String colortype) {return NULL; }}

7. Create a factory manufacturer to generate a specific plant

 Public classFactoryproducer { Publicabstractfactory getfactory (String factorytype) {if(Factorytype = =NULL){          return NULL; }      if(Factorytype.equalsignorecase ("Color_factory")) {        return Newcolor_factory (); }      if(Factorytype.equalsignorecase ("Shape_factory")) {            return Newshape_factory (); }      return NULL; }}

8. User

 Public class User {    publicstaticvoid  main (string[] args) {        new factoryproducer ();                Factoryproducer.getfactory("Color_factory"). GetColor ("Red"). Draw ();        Factoryproducer.getfactory ("Shape_factory"). Getshape ("Circle"). Draw ();            }}

Java design pattern-Abstract Factory mode

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.