The Java Learning Factory method model

Source: Internet
Author: User

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

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.