Single-Case mode:
public class Person2 {// a hungry man mode//private static Person2 person = new Person2 ();//private Person2 () {}//public static Per Son2 Getinstacne () {//return person;//}//static inner class--and external class to invoke//static--as the class loads and generates private static class personholder{ private static final Person2 INSTANCE = new Person2 ();} Private Person2 () {}public static Person2 GetInstance2 () {return personholder.instance;} lazy mode//private static Person2 person2;//private Person2 () {}//public static synchronized Person2 getinstace () {// if (person==null) {//person = new Person2 ();//}//return person;//}}
The factory method takes a class inheritance mechanism, generates a subclass, overrides the factory method, and generates an object within the method.
Abstract factories employ an object-grouping mechanism that specifically defines "factory" objects to be responsible for the creation of objects
Simple Use cases:
interface icar {void Docar ();} Class BWM implements icar{@Overridepublic void Docar () {System.out.println ("BWM");}} Class BK implements icar{@Override//Factory method takes a class inheritance mechanism (generates a subclass, overrides the factory method, produces an object in the method) public void Docar () {System.out.println ("BK");}} Interface Icarfac{icar Createcar ();} Class BWMFAC implements icarfac{@Override//Abstract factory employs an object composition mechanism that specifically defines a "factory" object to be responsible for creating the object public BWM Createcar () {return new BWM ();}} Class BKFAC implements icarfac{@Overridepublic BK Createcar () {return new BK ();}} Abstract factory for multiple product grades results public class Test{public static void Main (string[] args) {ICARFAC MFAC = new Bwmfac (); icar m = mfac.create Car ();//The factory class is responsible for creating an instance of the specific subclass of the abstract product M.docar (); MFAC = new Bkfac (); icar k = Mfac.createcar (); K.docar ();}}
Singleton mode/factory method mode/abstract Factory mode