Core Role:
--Instantiate the object and replace the new operation with a factory method
--Will select the implementation class, the creation of key objects unified management and control, so that the caller and our implementation class decoupling
Common Application Scenarios:
  
   Simple Factory mode:
--The Simple Factory mode is also called the Static Factory mode, that is, the factory class usually uses static methods, and returns different object instances by accepting different parameters.
--Powerless to add new products! Do not modify the factory class code is not, can not be extended violation of the opening and closing principle
  Code:
 Package com.lp.factory.simplefactory;  Public Interface Car {    void  run ();}
car Interface Class
 Package com.lp.factory.simplefactory;  Public class Implements car{    @Override    publicvoid  run () {        System.out.println ("Audi is running" );    }}  Public class Implements Car {    @Override    publicvoid  run () {        System.out.println ( "BYD is running");}    }
sub-class of car
 Packagecom.lp.factory.simplefactory;ImportCom.lp.factory.factorymethod.Audi;ImportCom.lp.factory.factorymethod.Byd;ImportCom.lp.factory.factorymethod.Car;//This is a simple factory. When modifying a car subclass, this class does not conform to the opening and closing principle . Public classCarfactory { Public StaticCar Creatcar (String type) {if("Audi". Equals (Type) {            return NewAudi (); }Else if("Byd". Equals (Type) {            return NewByd (); }Else{            return NULL; }    }}Factory class
 Package com.lp.factory.simplefactory; Import Com.lp.factory.factorymethod.Car; // invoke Simple factory class  Public class Client02 {    publicstaticvoid  main (string[] args) {        = Carfactory.creatcar ("Audi");         = Carfactory.creatcar ("Byd");        C1.run ();        C2.run ();    }}
Client
Factory method Mode:
--to avoid the shortcomings of the simple factory model, not fully meet the OCP
The biggest difference between a factory method pattern and a simple factory is that the simple factory model has only one (for a project or a standalone module) a factory class, and a factory method pattern that implements the same interface as the factory class
Code:
 Package Com.lp.factory.factorymethod;  Public Interface Car {    void  run ();}
car Interface Class
 PackageCom.lp.factory.factorymethod; Public classAudiImplementscar{@Override Public voidrun () {System.out.println ("Audi is running."); }} PackageCom.lp.factory.factorymethod; Public classBenzImplementsCar {@Override Public voidrun () {System.out.println ("Mercedes-Benz is running"); }    } PackageCom.lp.factory.factorymethod; Public classBydImplementsCar {@Override Public voidrun () {System.out.println ("BYD is running"); }}Car Implementation Class
 Package Com.lp.factory.factorymethod;  Public Interface carfactory {    Car creatcar ();}
Factory Interface Class
 PackageCom.lp.factory.factorymethod; Public classAudifactoryImplementscarfactory {@Override PublicCar Creatcar () {return NewAudi (); }} Public classBenzfactoryImplementscarfactory{@Override PublicCar Creatcar () {return NewBenz (); }} Public classBydfactoryImplementscarfactory {@Override PublicCar Creatcar () {return NewByd (); }}sub-class of factory implementation
 Package Com.lp.factory.factorymethod;  Public class  publicstaticvoid  main (string[] args) {     new  Audifactory (). Creatcar ();      New bydfactory (). Creatcar ();      New benzfactory (). Creatcar ();     C1.run ();     C2.run ();      C3.run (); }}
Client
Abstract Factory mode:
--to produce all products of different product families. (Support for adding product families for new additions)
--Abstract Factory mode is an upgraded version of the factory method pattern, where there are multiple business varieties, the business classification is that using the abstract factory pattern to produce the required objects is a very good solution
Code:
 Packagecom.lp.factory.abstractfactory; Public InterfaceEngine {voidrun (); voidstart ();}classLuxuryengineImplementsengine{@Override Public voidrun () {System.out.println ("Go fast."); } @Override Public voidstart () {System.out.println ("Start fast!" can start and stop automatically "); }    }classLowengineImplementsengine{@Override Public voidrun () {System.out.println ("Turn slowly!" "); } @Override Public voidstart () {System.out.println ("Slow Start"); }    }engine interface and its implementation class
 Packagecom.lp.factory.abstractfactory; Public InterfaceSeat {voidmessage ();}classLuxuryseatImplementsSeat {@Override Public voidmessage () {SYSTEM.OUT.PRINTLN ("Very relaxing"); }    }classLowseatImplementsSeat {@Override Public voidmessage () {SYSTEM.OUT.PRINTLN ("Not a massage"); }    }seat interface and its implementation class
 Packagecom.lp.factory.abstractfactory; Public InterfaceType {voidrevolve ();}classLuxurytypeImplementstype{@Override Public voidrevolve () {System.out.println ("Little Wear"); }    }classLowtypeImplementstype{@Override Public voidrevolve () {System.out.println ("Great Wear"); }    }Tire interface and its realization class
 Package com.lp.factory.abstractfactory;  Public Interface carfactory {    Engine createngine ();    Seat creatseat ();    Type Creattype ();}
Factory Method Interface
 Packagecom.lp.factory.abstractfactory; Public  classLowcarfactoryImplementscarfactory {@Override PublicEngine Createngine () {//TODO Auto-generated method stubs        return NewLowengine (); } @Override PublicSeat creatseat () {//TODO Auto-generated method stubs        return Newlowseat (); } @Override PublicType Creattype () {//TODO Auto-generated method stubs        return NewLowtype (); }}low-level car production plant
 Packagecom.lp.factory.abstractfactory; Public classLuxurycarfactoryImplementscarfactory {@Override PublicEngine Createngine () {return NewLuxuryengine (); } @Override PublicSeat creatseat () {return Newluxuryseat (); } @Override PublicType Creattype () {return NewLuxurytype (); }}Advanced Automotive Production Plant
 Package com.lp.factory.abstractfactory;  Public class Client {    publicstaticvoid  main (string[] args) {        new  luxurycarfactory ();         = cf.createngine ();        E.run ();        E.start ();    }}
Client
Factory mode Highlights:
--Simple Factory mode (static Factory)
    Although some degree does not accord with the design principle, but actually uses most.
--factory Method mode
    The extension is implemented by adding new factory classes without modifying existing classes
--Abstract Factory mode
    Can not increase product, can increase product family
Design mode (ii) Factory mode