JAVA and mode 2nd-factory method mode

Source: Internet
Author: User
Tags export class

The Factory method mode is the class creation mode, also known as the Virtual Constructor mode or the Polymorphic Factory mode.
The purpose of the factory method mode is to define a factory interface for creating product objects and postpone the actual creation to the subclass.

In what scenarios is the factory method used? The following is an example of my understanding:

I believe that many people have done the Import and Export function, taking the export function as an example. The XX system needs to support the export of employee salaries in the database and multiple formats such as HTML, CSV, and PDF. The exported structure of each format is different, for example: finance and others may have different requirements on the HTML format for salary export, because finance may need a specific format for easy accounting or other purposes.

If you use the simple factory model, the factory class must be too bloated. Because the simple factory mode has only one factory class, it needs to process all the created logic. If the above requirements only support three export formats and two export structures, then the factory class requires six if else to create six different types. If demand increases in the future, the consequences will be unimaginable.

At this time, the factory method mode is required to deal with the above requirements. In the factory method mode, the core factory class is no longer responsible for the creation of all objects, but the specific creation work is handed over to the subclass. This core class changes and becomes an abstract factory role. It is only responsible for providing the interface that must be implemented by the specific factory subclass without touching the details of which class should be instantiated.

This further abstract result allows the factory method mode to allow the system to introduce new products without modifying the specific factory role, this feature undoubtedly makes the factory method model more advantageous than the simple factory model. The following is a UML diagram for the above requirements:

 

 

It can be seen that the system using the factory method model involves the following roles:

 

The abstract factory role is the core of the factory method mode. Any factory class that creates objects in the mode must implement this interface. In actual systems, this role is often implemented using abstract classes.

Specific Factory (ExportHtmlFactory, exportpdffacloud) role: the role is a specific JAVA class that implements the abstract factory interface. The specific factory role contains the logic closely related to the business and is called by the user to create an export class (for example, ExportStandardHtmlFile ).

ExportFile role: the superclass of the object created in the factory method mode, that is, the common parent class of all the export classes or the common interfaces. In actual systems, this role is often implemented using abstract classes.

ExportStandardHtmlFile: this role implements the interface declared by the abstract Export (ExportFile) role. Every object created in the factory method mode is an instance of a specific export role.

Source code
The first is to abstract the source code of the factory role. It declares a factory method that requires all specific factory roles to implement this factory method. The type parameter indicates the structure of the exported format. For example, the exported HTML format has two structures: standard structure and financial structure.

[Java]
Package com. bankht. factoryMethod;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:53:10
*
* @ Class description: Abstract Factory role
*/
Public interface ExportFactory {
Public ExportFile factory (String type );
}
Package com. bankht. factoryMethod;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:53:10
*
* @ Class description: Abstract Factory role
*/
Public interface ExportFactory {
Public ExportFile factory (String type );
}
 

Specific factory corner class source code:

[Java]
Package com. bankht. factoryMethod;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:54:54
*
* @ Class description: specific factory role categories
*/
Public class ExportHtmlFactory implements ExportFactory {
 
@ Override
Public ExportFile factory (String type ){
// TODO Auto-generated method stub
If ("standard". equals (type )){
 
Return new ExportStandardHtmlFile ();
 
} Else if ("financial". equals (type )){
 
Return new ExportFinancialHtmlFile ();
 
} Else {
Throw new RuntimeException ("no object found ");
}
}
 
}
Package com. bankht. factoryMethod;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:54:54
*
* @ Class description: specific factory role categories
*/
Public class ExportHtmlFactory implements ExportFactory {

@ Override
Public ExportFile factory (String type ){
// TODO Auto-generated method stub
If ("standard". equals (type )){

Return new ExportStandardHtmlFile ();

} Else if ("financial". equals (type )){

Return new ExportFinancialHtmlFile ();

} Else {
Throw new RuntimeException ("no object found ");
}
}

}

 

[Java]
Package com. bankht. factoryMethod;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:55:04
*
* @ Class description: specific factory role categories
*/
Public class ExportPdfFactory implements ExportFactory {
 
@ Override
Public ExportFile factory (String type ){
If ("standard". equals (type )){
 
Return new ExportStandardHtmlFile ();
 
} Else if ("financial". equals (type )){
 
Return new ExportFinancialHtmlFile ();
 
} Else {
Throw new RuntimeException ("no object found ");
}
}
 
}
Package com. bankht. factoryMethod;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:55:04
*
* @ Class description: specific factory role categories
*/
Public class ExportPdfFactory implements ExportFactory {

@ Override
Public ExportFile factory (String type ){
If ("standard". equals (type )){

Return new ExportStandardHtmlFile ();

} Else if ("financial". equals (type )){

Return new ExportFinancialHtmlFile ();

} Else {
Throw new RuntimeException ("no object found ");
}
}

}

Abstract export the source code of the role class:

[Java]
Package com. bankht. factoryMethod;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:55:47
*
* @ Class description: Abstract Export File class
*/
Public interface ExportFile {
Public boolean export (String data );
}
Package com. bankht. factoryMethod;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:55:47
*
* @ Class description: Abstract Export File class
*/
Public interface ExportFile {
Public boolean export (String data );
}
 

Export the source code of the role class. Generally, this class has complicated business logic.

Finance:

[Java]
Package com. bankht. factoryMethod;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:54:54
*
* @ Class description: export the finance HTML file
*/
Public class exportpolicalhtmlfile implements ExportFile {
 
@ Override
Public boolean export (String data ){
// TODO Auto-generated method stub
/**
* Business logic
*/
System. out. println ("Export Finance HTML file ");
Return true;
}
 
}
Package com. bankht. factoryMethod;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:54:54
*
* @ Class description: export the finance HTML file
*/
Public class exportpolicalhtmlfile implements ExportFile {

@ Override
Public boolean export (String data ){
// TODO Auto-generated method stub
/**
* Business logic
*/
System. out. println ("Export Finance HTML file ");
Return true;
}

}

 

[Java]
Package com. bankht. factoryMethod;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:54:54
*
* @ Class description: export the finance PDF File
*/
Public class exportpolicalpdffile implements ExportFile {
 
@ Override
Public boolean export (String data ){
// TODO Auto-generated method stub
/**
* Business logic
*/
System. out. println ("Export Finance PDF file ");
Return true;
}
 
}
Package com. bankht. factoryMethod;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:54:54
*
* @ Class description: export the finance PDF File
*/
Public class exportpolicalpdffile implements ExportFile {

@ Override
Public boolean export (String data ){
// TODO Auto-generated method stub
/**
* Business logic
*/
System. out. println ("Export Finance PDF file ");
Return true;
}

}

Normal:

[Java]
Package com. bankht. factoryMethod;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:54:54
*
* @ Class description: export standard HTML files
*/
Public class ExportStandardHtmlFile implements ExportFile {
 
@ Override
Public boolean export (String data ){
// TODO Auto-generated method stub
/**
* Business logic
*/
System. out. println ("export standard HTML file ");
Return true;
}
 
}
Package com. bankht. factoryMethod;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:54:54
*
* @ Class description: export standard HTML files
*/
Public class ExportStandardHtmlFile implements ExportFile {

@ Override
Public boolean export (String data ){
// TODO Auto-generated method stub
/**
* Business logic
*/
System. out. println ("export standard HTML file ");
Return true;
}

}

 

[Java]
Package com. bankht. factoryMethod;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:54:54
*
* @ Class description: Export Standard PDF files
*/
Public class ExportStandardPdfFile implements ExportFile {
 
@ Override
Public boolean export (String data ){
// TODO Auto-generated method stub
/**
* Business logic
*/
System. out. println ("Export Standard PDF file ");
Return true;
}
 
}
Package com. bankht. factoryMethod;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:54:54
*
* @ Class description: Export Standard PDF files
*/
Public class ExportStandardPdfFile implements ExportFile {

@ Override
Public boolean export (String data ){
// TODO Auto-generated method stub
/**
* Business logic
*/
System. out. println ("Export Standard PDF file ");
Return true;
}

}

 

Client Angle class source code:

[Java]
Package com. bankht. factoryMethod;
 
Import org. junit. Test;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:59:25
*
* @ Class description: Test Factory method mode class
*/
Public class TestFactoryMethod {
 
@ Test
Public void testFactoryMethod (){
String data = "";
ExportFactory exportFactory = new ExportHtmlFactory ();
ExportFile ef = exportFactory. factory ("financial ");
Ef. export (data );
}
 
}
Package com. bankht. factoryMethod;

Import org. junit. Test;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:59:25
*
* @ Class description: Test Factory method mode class
*/
Public class TestFactoryMethod {

@ Test
Public void testFactoryMethod (){
String data = "";
ExportFactory exportFactory = new ExportHtmlFactory ();
ExportFile ef = exportFactory. factory ("financial ");
Ef. export (data );
}

}
 

Activity sequence diagram of factory method mode

 


When the client creates an ExportHtmlFactory object, the static type of the variables held by the client is ExportFactory, and the actual type is ExportHtmlFactory. Then the client calls the factory method factory () of the ExportHtmlFactory object, and the latter calls the constructor of exportexternalhtmlfile to create the export object.

Author: m13666425773
 

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