Java design pattern-Abstract Factory mode

Source: Internet
Author: User

Role:

Abstract Factory Role : Assuming this role is the core of the pattern, he should be independent of the application of the business system. Usually Java interfaces or Java abstract classes, other specific factory classes must implement this Java interface or inherit this Java abstract class.

Specific Factory role : This role creates an instance of the product directly from the client's invocation, which contains logic to select the appropriate product object, which is closely related to the application's business logic.

Abstract Product Role : This role is the parent class of objects created by the factory pattern, or their common interface.

Specific product roles : Abstract Factory mode any product object that is created is an instance of a specific product class. This is what the client ultimately needs, and its interior is full of business logic.

Class structure diagram:

Fruit Abstraction Interface:

 Package com.design.abstractfactory;  Public Interface Fruit {    void  fruitinfo ();}

Vegetable Abstraction Interface:

 Package com.design.abstractfactory;  Public Interface Vegetable {    void  vegetableinfo ();}

Factory Abstract Interface:

 Package com.design.abstractfactory;  Public Interface Factory {    Fruit createfruit ();    Vegetable createvegetable ();}

Concrete class: Northern Fruit

 Package com.design.abstractfactory;  Public class Implements Fruit {    @Override    publicvoid  fruitinfo () {        System.out.println ("North-apple");}    }

Specific Category: Southern fruit

 Package com.design.abstractfactory;  Public class Implements Fruit {    @Override    publicvoid  fruitinfo () {        System.out.println ( "South-banana");}    }

Specific categories: Northern vegetables

 Package com.design.abstractfactory;  Public class Implements Vegetable {    @Override    publicvoid  vegetableinfo () {        System.out.println ("North-tomato");}    }

Specific categories: Southern Vegetables

 Package com.design.abstractfactory;  Public class Implements Vegetable {    @Override    publicvoid  vegetableinfo () {        System.out.println ("South-taro");}    }

Concrete class: North Product Factory

 Package com.design.abstractfactory;  Public class Implements Factory {    @Override    public  Fruit createfruit () {        returnNew  northfruit ();    }    @Override    public  Vegetable createvegetable () {        returnNew  northvegetable ();    }}

Specific categories: Southern Products Factory

 Package com.design.abstractfactory;  Public class Implements Factory {    @Override    public  Fruit createfruit () {        returnNew  southfruit ();    }    @Override    public  Vegetable createvegetable () {        returnNew  southvegetable ();    }}

Test class:

 Packagecom.design.abstractfactory; Public classClient { Public Static voidMain (string[] args) {Factory northfactory=Newnorthproductfactory (); Factory southfactory=Newsouthproductfactory (); System.out.println ("Northern Products:");        Northfactory.createfruit (). Fruitinfo ();        Northfactory.createvegetable (). Vegetableinfo (); System.out.println ("Southern Products:");        Southfactory.createfruit (). Fruitinfo ();    Southfactory.createvegetable (). Vegetableinfo (); }}

Output:

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.