Java design mode -- factory method)
The factory method mode is the class creation mode. It is intended to define a factory interface for creating product objects and postpone the actual creation to the subclass.
I believe that many people have done the Import and Export function, taking the export function as an example. There is a need: a system needs to support the export of employee salaries in the database, and support multiple formats such as HTML, Excel, PDF, etc. 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.
The UML design diagram is as follows:
public interface ExportFactory { public ExportFile factory(String type);}
public interface ExportFile { public boolean export(String data);}
Public class ExportHtmlFactory implements ExportFactory {public ExportFile factory (String type) {if ("standard ". equals (type) {return new ExportStandardHtmlFile ();} else if ("finacial ". equals (type) {return new ExportFinacialHtmlFile () ;}else {throw new RuntimeException ("no object found ");}}}
Public class ExportPdfFactory implements ExportFactory {public ExportFile factory (String type) {if ("standard ". equals (type) {return new ExportStandardPdfFile ();} else if ("finacial ". equals (type) {return new ExportFinacialPdfFile () ;}else {throw new RuntimeException ("no object found ");}}}
Public class ExportFinacialHtmlFile implements ExportFile {public boolean export (String data) {System. out. println ("export Finance HTML template! "); Return true ;}}
Public class ExportFinacialPdfFile implements ExportFile {public boolean export (String data) {System. out. println ("export Finance 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 Fu yuwei * @ time 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 many people. The export function is implemented by you, but the user (who calls this method) may be others. At this time, you should be flexible enough to minimize the coupling between the two. When you modify or add a new function, users do not need to modify anything. If your design is not flexible enough, you will not be able to face the changing needs of customers. A minor requirement change may change your code structure and cause others who use the interfaces you provide to modify their code. This makes it difficult to maintain the system in the future.
Simple factory Mode
The simple factory mode is an absolute product instance created by a factory object.
The class diagram is as follows: