Factory method Mode
The factory method pattern is the creation mode of the class. The factory method pattern is intended to define a factory interface that creates a product object, deferring the actual creation of the factory to subclasses .
Factory method Mode
The factory method pattern is the result of further abstraction of the simple factory pattern.
If it is a factory method pattern that does not use reflection, then all the If ...
Else If...else are placed in the factory class, will inevitably cause the factory class of infinite bloated
This is where the factory approach pattern is needed to deal with the problem. In the factory method model, the core factory class is no longer responsible for all object creation, but rather the creation of the specific work to the subclass to do. This class is transformed into an abstract factory role that is only responsible for the interfaces that a specific factory subclass must implement.
This improvement allows the system to introduce new products without modifying the specific factory roles.
Factory method Pattern Structure
Systems that use the factory method pattern involve the following roles:
1. Abstract Factory role
This role is at the heart of the factory method pattern, and any factory class that creates objects in the schema must implement this interface
2. Specific Factory role
This role is the specific Java class that implements the factory interface, and the specific factory roles are closely related to the business, and with the user's call to create the export class
3. Abstract Export role
Super class for objects created by factory method mode
4. Specific Export roles
This role implements the interface declared by the abstract export role, and each object created by the factory method pattern is an instance of a specific role export role
Example of a factory method pattern
There is an abstract factory that exports files to the factory:
Public Interface exportfactory{ public exportfile Factory (String type);
It has two implementation classes, the factory that exports the HTML file, and the factory that exports the PDF file:
Public classExporthtmlfactoryImplementsexportfactory{ Publicexportfile Factory (String type) {if("Standard". Equals (Type) { return NewExportstandardhtmlfile (); } Else if("Financial". Equals (Type) { return NewExportfinancialhtmlfile (); } return NULL; }}
Public classExportpdffactoryImplementsexportfactory{ Publicexportfile Factory (String type) {if("Standard". Equals (Type) { return NewExportstandardpdffile (); } Else if("Financial". Equals (Type) { return NewExportfinancialpdffile (); } return NULL; }}
Abstract product role, an export file:
Public Interface exportfile{ publicboolean export (String data);
Specific product roles:
public class Exportfinancialhtmlfile implements Exportfile { public boolean export (String data) {System.out.println ( "Exporting the Financial HTML file" return false ; }}
public class Exportfinancialpdffile implements exportfile{ public boolean export (String data) {System.out.println ( "Exporting the financial PDF file" return true ; }}
public class Exportstandardhtmlfile implements exportfile{ public boolean export (String data) {System.out.println ( "Exports the standard HTML file" ); return true ; }}
Public class Implements exportfile{ publicboolean export (String data) { System.out.println ( "Export standard PDF file"); return true ; }}
Impersonate a client call, instantiate a specific factory role, and return different product roles based on the parameters passed in:
Public Static void Main (string[] args) { = ""; New exporthtmlfactory (); = Exportfactory.factory ("financial"); Ef.export (data);}
The returned result is:
Export the financial version HTML file
Of course, you can also modify the code to see what you like, instantiate a different subclass and call the export method to print.
Summarize
The design of the system should be flexible enough to minimize the coupling between the code, when modifying or adding a new function, so that the user as far as possible to modify a few places. If the design is not flexible enough, it will not be able to face the changing needs, perhaps a minimal change in requirements, will make the code structure changes, and other users will have to modify their code. Reaching, the system will be difficult to maintain in the future.
Java design mode 3: Factory method mode