Java Design pattern-factory method mode (factory methods)

Source: Internet
Author: User

The factory method pattern is the class's creation pattern, which is intended to define a factory interface that creates product objects, deferring the actual creation to subclasses.
Believe that many people have done import and export functions, take the export function. There is a need: a system needs to support the export of payroll in the database, and support a variety of formats such as: HTML, Excel, PDF, etc., the structure of the export of each format is different, such as: financial and other people on the export pay HTML format requirements may be different, Because finance may require 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 model has only one factory class, it needs to handle all the created logic. If the above requirements temporarily support only 3 export formats and 2 export structures, then the factory class requires 6 if else to create 6 different types. If demand increases in the future, the consequences will be dire.
This is where the factory approach model is needed to handle the above requirements. In factory method mode, the core factory class is no longer responsible for the creation of all objects, but rather the creation of the work is given to subclasses to do. This core class is transformed into an abstract factory role that is only responsible for giving the interfaces that a specific factory subclass must implement, without touching which class should be instantiated in this detail.
The UML design diagram is as follows:

publicinterface ExportFactory {    publicfactory(String type);}
publicinterface ExportFile {    publicexport(String data);}
 Public  class exporthtmlfactory implements exportfactory {     PublicExportfileFactory(String type) {if("Standard". Equals (type)) {return NewExportstandardhtmlfile (); }Else if("Finacial". Equals (type)) {return NewExportfinacialhtmlfile (); }Else{Throw NewRuntimeException ("No objects Found"); }    }}
 Public  class exportpdffactory implements exportfactory {     PublicExportfileFactory(String type) {if("Standard". Equals (type)) {return NewExportstandardpdffile (); }Else if("Finacial". Equals (type)) {return NewExportfinacialpdffile (); }Else{Throw NewRuntimeException ("No objects Found"); }    }}
public   class  exportfinacialhtmlfile  implements  exportfile  { public  boolean  export  (String data) {System.out.println ( "Export Financial HTML template!        "); return     true ; }}
public   class  exportfinacialpdffile  implements  exportfile  { public  boolean  export  (String data)        {System.out.println ( "Export Financial PDF template" ); return     true ; }}
public   class  exportstandardhtmlfile  implements  exportfile  { public  boolean  export  (String data)        {System.out.println ( "Export standard HTML Template" ); return     true ; }}
public   class  exportstandardpdffile  implements  exportfile  { public  boolean  export  (String data)        {System.out.println ( "Export standard PDF template" ); return     true ; }}
/** * @author  Shu Yuwei * @time  2015-3-17 pm 08:57:11 * @param  args */ public  static  void  main  (string[] args)        {String data = ;        Exportfactory exportfactory = new  exporthtmlfactory ();        Exportfile exportfile = exportfactory.factory ( "finacial" );    Exportfile.export (data); }}

An application system is developed by multiple people, and the exported function is implemented by you, but the user (the person who called the method) may be someone else. At this point you should be flexible enough to minimize the coupling between the two, and when you modify or add a new feature, the user does not need to modify it anywhere. If your design is not flexible enough, you will not be able to face the changing needs of customers. Perhaps a minimal requirement change will change your code structure and cause other people who use the interface you provide to modify their code. Take one place and move the whole body, which makes the system difficult to maintain in the future.
Simple Factory mode
Simple Factory mode is a product instance that is absolutely created by a factory object
The class diagram is as follows:

Java Design pattern-factory method mode (factory methods)

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.