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