The meaning of the factory method pattern is to define a factory interface that creates the product objects, deferring the actual creation to subclasses.
The Core factory class is no longer responsible for product creation, so that the core class becomes an abstract factory role and is responsible only for the interfaces that the specific factory subclasses must implement.
The benefit of this further abstraction is that the factory method model allows the system to introduce new products without modifying the specific factory roles.
Original link http://tech.it168.com/a2009/0223/266/000000266400.shtml
Package COM.FYLX;//define an interface that peopleInterfacepeople{ Public voidSpeaking ();}//define a Chinese groupclassChina implements people{ Public voidSpeaking () {System. out. println ("I am a Chinese"); }}//define a Japanese groupclassJapan implements people{ Public voidSpeaking () {System. out. println ("にほんごん"); }}//Factory classclassfactory{ Public Staticpeople getinstance (String className) {people people=NULL; Try{people=(People) Class.forName (className). newinstance (); } Catch(Exception e) {e.printstacktrace (); } returnpeople; }} Public classfactorymethod{ Public Static voidMain (string[] args) {people F= Factory.getinstance ("Com.fylx.Japan"); if(F! =NULL) {f.speaking (); } }}
The main is the application of interface inheritance and reflection. There are many open source components that use factory methods in Java, such as quartz,log4j,hibernate, etc.
The Java Learning Factory method model