Java design mode-Factory mode
The factory model is divided into simple factory, factory method mode and abstract Factory mode. The simple factory as the name implies is the simplest, from a factory to obtain the desired product similar to Factory.getproduct1 (), or factory.getproduct2 (), the most classic use of the switch statement. Simple factory to increase the product to modify the source code, to undermine the OCP principle (open to expansion, modify the closed)
The factory method pattern is the most different from a simple factory. Factory method mode with factory interface
The biggest difference between the abstract factory model and the factory method pattern is that the products of the factory method pattern are derived from the same interface or abstract class, and the products of the abstract factory pattern derive from different interfaces or abstract classes. Because the factory method pattern is for a product hierarchy, the abstract factory approach is for more than one interface (abstract class) and multiple interfaces (abstract classes), respectively. What product grade structure? For example, such as cars, sub-Audi and BMW, while Audi and BMW are divided into large displacement and small displacement, then the large-displacement Audi and small-displacement Audi belong to the same product grade structure, and large-displacement Audi and large-displacement BMW belong to the same product family. If for a product grade structure, that is, Audi or BMW, then the factory products for a product grade structure using the factory method mode, if targeting multiple, such as BMW and Audi, then use the abstract factory model, its product is a product family. This is their application scenario.
Here is an example of a familiar abstract factory pattern. (use singleton mode to avoid a lot of factory creation, waste of resources, do not understand that there is a single example)
Public Interface Ifactorydao { public Iuserdao Createuserdao (); Public Iaddressdao Createaddressdao (); } //
Public InterfaceIaddressdao { Public voidAdd (address address,intuserId); Public voidUpdate (address address); Public voidDeleteintID); }//Product 1 Interface Public InterfaceIuserdao { Public voidAdd (user user); Public voidDeleteintID); Public voidUpdate (user user); }//Product 2 Interface Public classUserjdbcdaoImplementsIuserdao {@Override Public voidAdd (user user) {System.out.println ("Userjdbcdao....add"); } @Override Public voidDeleteintID) {SYSTEM.OUT.PRINTLN ("Userjdbcdao....delete"); } @Override Public voidUpdate (user user) {System.out.println ("Userjdbcdao....update"); } }//Product 2 implementation for JDBC Public classAddressjdbcdaoImplementsIaddressdao {@Override Public voidAdd (address address,intuserId) {System.out.println ("Addressjdbcdao....add"); } @Override Public voidUpdate (address) {SYSTEM.OUT.PRINTLN ("Addressjdbcdao....update"); } @Override Public voidDeleteintID) {SYSTEM.OUT.PRINTLN ("Addressjdbcdao....delete"); } }//Product 1 implementation for JDBC Public classJdbcdaofactoryImplementsIfactorydao {Private StaticJdbcdaofactory factory =Newjdbcdaofactory (); Privatejdbcdaofactory () {} Public StaticIfactorydao getinstance () {returnFactory; } @Override PublicIuserdao Createuserdao () {return NewUserjdbcdao (); } @Override PublicIaddressdao Createaddressdao () {return NewAddressjdbcdao (); } }//Factory Interface Implementation 1 (JDBC) Public classUsermysqldaoImplementsIuserdao {@Override Public voidAdd (user user) {System.out.println ("Usermysqldao....add"); } @Override Public voidDeleteintID) {SYSTEM.OUT.PRINTLN ("Usermysqldao....delete"); } @Override Public voidUpdate (user user) {System.out.println ("Usermysqldao....update"); } }//2 Implementation of MySQL-based products Public classAddressmysqldaoImplementsIaddressdao {@Override Public voidAdd (address address,intuserId) {System.out.println ("Addressmysqldao....add"); } @Override Public voidUpdate (address) {SYSTEM.OUT.PRINTLN ("Addressmysqldao...update"); } @Override Public voidDeleteintID) {SYSTEM.OUT.PRINTLN ("Addressmysqldao....delete"); } }//1 Implementation of MySQL-based products
Factory Implementation 2 (MYSQL)
Public class Implements Ifactorydao {
Private StaticIfactorydao factory =Newmysqldaofactory (); Privatemysqldaofactory () {} Public StaticIfactorydao getinstance () {returnFactory; } @Override PublicIaddressdao Createaddressdao () {return NewAddressmysqldao (); } @Override PublicIuserdao Createuserdao () {return NewUsermysqldao (); } }
Two plants can be found, two of the products in the factory are product families (Addressmysqldao and Addressjdbcdao are the same interface implementation)
--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------
Calling Factory 1 or 2 in your project also modifies the code, which is not good, so you can use reflection. Write the factory classname you want to create in the configuration file so that you can only modify the configuration file, flexible, non-destructive encapsulation, OCP.
[Java]View PlainCopy
- Package Com.yan.factory.dao;
- Import java.lang.reflect.InvocationTargetException;
- Import Java.lang.reflect.Method;
- Import java.util.Properties;
- Public class Daoutil {
- public static Ifactorydao createdaofactory () {
- Ifactorydao f = null;
- try {
- Properties prop = Propertiesutil.getdaoprop ();
- String fs = Prop.getproperty ("Factory");
- Class CLZ = Class.forName (FS);
- String mn = "getinstance";
- Method m = Clz.getmethod (MN);
- f = (Ifactorydao) m.invoke (CLZ);
- } catch (ClassNotFoundException e) {
- E.printstacktrace ();
- } catch (SecurityException e) {
- E.printstacktrace ();
- } catch (Nosuchmethodexception e) {
- E.printstacktrace ();
- } catch (IllegalArgumentException e) {
- E.printstacktrace ();
- } catch (Illegalaccessexception e) {
- E.printstacktrace ();
- } catch (InvocationTargetException e) {
- E.printstacktrace ();
- }
- return F;
- }
- }
Dao.properties file
- Factory=com.yan.factory.dao.jdbcdaofactory
You can modify the configuration file test.
In the project private Iaddressdao Addressdao = Daoutil.createdaofactory (). Createaddressdao ();
--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------
There is a little drawback is too complicated, to create multiple factory instances, we can use reflection, just create a factory, write the DAO to be created in the configuration file
PackageCom.yan.factory.dao; ImportJava.util.HashMap; ImportJava.util.Map; Importjava.util.Properties; Public classPropertiesfactoryImplementsIfactorydao {Private StaticPropertiesfactory f =Newpropertiesfactory (); Privatepropertiesfactory () {} Public StaticIfactorydao getinstance () {returnF; } @Override PublicObject Getdao (String name) {Try{Properties prop=Propertiesutil.getdaoprop (); String cn=Prop.getproperty (name); Object obj=Class.forName (CN). newinstance (); System.out.println (obj); returnobj; } Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } return NULL; }} Factory=com.yan.factory.dao.PropertiesFactory Userdao= "Com.yan.factory.dao.UserJDBCDao"
Reprint: 54897088; If there is infringement, please contact delete.
Java design mode-Factory mode