Java design Pattern (3)--Abstract Factory mode

Source: Internet
Author: User

Abstract Factory mode is the most abstract and general of all forms of Factory mode. Abstract Factory mode provides the client with an interface that enables the client to create product objects for multiple product families without having to specify the specific type of product.

First, product family and product grade structure

In order to facilitate the introduction of the abstract factory model, a new concept has been introduced: product family Family. The so-called product family, is located in the different product hierarchy structure, the function is related to the product composition family . The number of products contained in each product family is equal to the number of product grade structures. The product hierarchy and product family divide the product into different directions, forming a two-dimensional coordinate system. That is, Phase diagram:

A specific factory role is added to the above phase diagram. It can be seen that each product family corresponds to a specific factory. Each factory is responsible for creating products that belong to the same product family, but are divided into different hierarchical institutions.

Ii. roles contained in the model and their responsibilities

1. Abstract Factory (Creator) role
The core of the abstract factory pattern, which contains a declaration of multiple product structures , must be implemented by any factory class.
2. Specific factory (concrete Creator) role
The specific factory class is an implementation of the abstract factory, which is responsible for instantiating product objects in a product family .
3. Abstract (Product) role
The parent class of all objects created by abstract mode, which is responsible for describing common interfaces common to all instances.
4. Specific products (concrete product) role
Concrete instance objects created by abstract mode
Summary : The method in the abstract factory corresponds to the product structure, and the specific factory corresponds to the product family.

Third, the demo abstract role

Product structure (Apple, Banana)

 Public Interface Fruit {    /     * *     collect * /public     void  get ( );}
 Public Abstract class Implements fruit{    /     * *     collect      */Public  Abstractvoid  get ();}
 Public Abstract class Implements fruit{    /     * *     capture * * Public     abstract void get ();}
Specific product roles

Product family (northern fruit)

 Public class extends Apple {    publicvoid  get () {        System.out.println ("Collect Northern Apples");}    }
 Public class extends Banana {    publicvoid  get () {        System.out.println ("collect Northern Bananas") );    }}

Product family (southern fruit)

 Public class extends Apple {    publicvoid  get () {        System.out.println ("collect Southern Apples");}    }
 Public class extends Banana {    publicvoid  get () {        System.out.println ("collect Southern Bananas") );    }}
Abstract Factory role
 Public Interface fruitfactory {    // instantiation of Applepublic      Fruit getapple ();     // Instantiate Banana     Public Fruit Getbanana ();}
Specific Factory roles
 public  class  northfruitfactory Span style= "color: #0000ff;" >implements   Fruitfactory { public   Fruit Getapple () { return  new   Northapple ();  public   Fruit Getbanana () { return  new   Northbanana (); }}
 Public class Implements fruitfactory {    public  Fruit getapple () {        returnnew  Southapple ();    }      Public Fruit Getbanana () {        returnnew  Southbanana ();}    }
Test class
 Public classMainClass { Public Static voidMain (string[] args) {fruitfactory ff=Newnorthfruitfactory (); Fruit Apple=ff.getapple ();                Apple.get (); Fruit Banana=Ff.getbanana ();                Banana.get (); Fruitfactory FF2=Newsouthfruitfactory (); Fruit Apple2=ff2.getapple ();                Apple2.get (); Fruit Banana2=Ff2.getbanana ();            Banana2.get (); }}
View Code

Console output

Collect the northern Apple collect the northern banana collect the southern Apple to collect the southern banana
Four, UML class diagram

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: 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.