Java factory model, java Factory

Source: Internet
Author: User

Java factory model, java Factory

The factory mode encapsulates the instantiation process of all sub-classes under an interface, which increases the scalability of the system and reduces the amount of code maintenance in the future.

Interface IPlug {void start ();} class TV implements IPlug {public void start () {System. out. println ("TV start") ;}} class AirCon implements IPlug {public void start () {System. out. println ("AirCon start") ;}} class Factory {public static IPlug getInstance (String className) {if ("TV ". equals (className) {return new TV ();} else if ("AirCon ". equals (className) {return new AirCon () ;}else {return null ;}} public class factoryDemo {public static void main (String [] args) {// TODO Auto-generated method stub IPlug ip = Factory. getInstance ("TV"); ip. start (); ip = Factory. getInstance ("AirCon"); ip. start ();}}

 

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.