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 ();}}