The factory method pattern also belongs to the class's creation pattern, also known as the polymorphic factory pattern . the meaning of the factory method pattern is to define a factory interface that creates the product objects, deferring the actual creation to subclasses. The Core factory class is no longer responsible for product creation, so that the core class becomes an abstract factory role and is responsible only for the interfaces that a specific factory subclass must implement, and the benefit of further abstraction is that the factory method pattern allows the system to introduce new products without modifying the specific factory roles.
I. Roles contained in the model and their responsibilities
1. Abstract Factory (Creator) role
At the core of the factory method pattern, any factory class must implement this interface.
2. Specific factory (concrete Creator)
Role-Specific factory class is an implementation of an abstract factory that is responsible for instantiating product objects .
3. Abstract (Product) role
The parent class of all objects created by the factory method pattern, which is responsible for describing the common interfaces common to all instances.
4. Specific products (concrete product) role
Specific instance objects created by the factory method pattern
Ii. Comparison of plant method models and simple factory models
- The structural differences between the factory method pattern and the simple factory model are not obvious. the core of the Factory method class is an abstract factory class , while the simple factory model places the core on a specific class.
- The factory method pattern has an alias called the Polymorphism factory pattern because the specific factory class has a common interface, or a common abstract parent class.
- When the system extension needs to add a new product object, just need to add a specific object and a specific factory object , the original factory objects do not need to make any changes, and do not need to modify the client, well in line with the " open-closed " principle. The simple factory model has to modify the factory method after adding the new Product object, the extensibility is not good.
- The factory method model can evolve into a simple Factory mode after degradation.
Third, the demo abstract role
Public Interface Fruit { / * * collect * /public void get ();}
Specific Products
public class Apple implements fruit{ /* * capture */ public void " Capture Apple "
Public class Implements fruit{ / * * Collect */public void get () { System.out.println ("collect Bananas");} }
Abstract Factory role
Public Interface fruitfactory { public Fruit getfruit ();}
Specific Factory
Public class Implements fruitfactory { public Fruit getfruit () { returnnew Apple ( ); }}
Public class Implements fruitfactory { public Fruit getfruit () { returnNew Banana (); }}
Test method
Public classMainClass { Public Static voidMain (string[] args) {//Get ApplefactoryFruitfactory FF =Newapplefactory (); //get Apple instance objects with ApplefactoryFruit Apple =Ff.getfruit (); Apple.get (); //Get BananafactoryFruitfactory FF2 =Newbananafactory (); Fruit Banana=Ff2.getfruit (); Banana.get (); }}
Console output
Collect apples to collect bananas
Four, UML class diagram
Java design Pattern (2)--factory method mode