1. Introduction
The factory model can be divided into classes:
1) Simple Factory mode (easy Factory)
2) Factory mode (Factory method)
3) Abstract Factory mode (Factory)
This pattern is progressively abstracted from top to bottom, and is more general, and usually looks at the simple factory pattern as a special case of the factory method pattern, which is grouped into one category.
2. Difference
Factory method Mode:
An abstract product class that can derive a number of specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can only create an instance of a specific product class.
Abstract Factory mode:
Multiple abstract product classes, each abstract product class can derive multiple specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can create instances of more than one specific product class.
That
The factory method pattern is only an abstract product class, while the abstract factory pattern has multiple.
Factory-mode-specific factory classes can only create instances of a specific product class, while abstract Factory mode may create multiple.
3. Example
1) Simple Factory mode
Product Category:
public class BMW {public BMW () { }}public class BMWX1 extends BMW {public BMWX1 () { System.out.println (" This is BMW X1 ");} public class BMWX7 extends BMW {public BMWX7 () { System.out.println ("The Is BMW X7");} }
Factory class:
public class Bmwfactory {public BMW CREATBMW (String type) { if (type.equals ("X1")) { return new BMWX1 (); c11/>} else if (Type.equals ("X7")) { return new BMWX7 (); } else { return null;}} }
Customer class:
public class Customer {public static void Main (string[] args) { Bmwfactory factory = new Bmwfactory (); BMW X1 = FACTORY.CREATBMW ("X1"); BMW X7 = FACTORY.CREATBMW ("X7");} }
For each additional type of vehicle, a if-else condition statement is added, violating the principle of opening to the extension and closing the modification
2) Factory method mode:
Product Category:
public class BMW {public BMW () { }}public class BMWX1 extends BMW {public BMWX1 () { System.out.println (" BMW X1 ");} } public class BMWX7 extends BMW {public BMWX7 () { System.out.println ("BMW X7");} }
Factory class:
Public interface Bmwfactory { BMW createbmw ();} public class Bmwx1factory implements Bmwfactory { @Override public BMW createbmw () { return new BMWX1 (); C12/>}}public class Bmwx7factory implements Bmwfactory { @Override public BMW createbmw () { return new BMWX7 (); }}
Customer class:
public class Customer {public static void Main (string[] args) { bmwx1factory x1 = new Bmwx1factory (); X1.CREATEBMW (); Bmwx7factory x7 = new Bmwx7factory (); X7.CREATEBMW (); }}
The client program only deals with interfaces provided by abstract product roles, but the number of objects increases exponentially. When the product category is very long, there will be a large number of corresponding plant objects.
3) Abstract Factory mode:
Product Category:
Public interface Engine {}public class Enginea implements engine {public Enginea () { System.out.println ("engine A ");} } public class Engineb implements engine {public Engineb () { System.out.println ("Engine B");} } Public interface Aircondition {}public class Airconditionc implements aircondition {public airconditionc () { System.out.println ("Aircondition C");} } public class Airconditiond implements aircondition {public Airconditiond () { System.out.println (" Aircondition D "); }}
Factory class:
Public interface Abstractfactory {public Engine selectengine (); Public aircondition selectaircondition ();} public class FactoryBMWX1 implements Abstractfactory { @Override public Engine selectengine () { return new Enginea (); } @Override public aircondition selectaircondition () { return new Airconditionc ();} } public class FactoryBMWX7 implements abstractfactory{ @Override public Engine selectengine () { return New Engineb (); } @Override public aircondition selectaircondition () { return new Airconditiond ();} }
Customer class:
public class Customer {public static void Main (string[] args) { FactoryBMWX1 x1 = new FactoryBMWX1 (); X1.selectengine (); X1.selectaircondition (); FactoryBMWX7 x7 = new FactoryBMWX7 (); X7.selectengine (); X7.selectaircondition (); }}
Multiple factory roles are available for multiple product roles, each specific factory role is only instantiated for one product role, and each specific factory class is responsible for creating an instance of a specific subclass of the abstract product.
4. Summary
No matter what kind of factory model, in the form and characteristics are very similar, the ultimate goal is to understand the decoupling. In use, it is not necessary to care about which model is specific, because the evolution between them is often elusive. Often you will find that the factory method is clearly used, when the new requirements come, slightly modified, adding a new method, because the products in the class constitute a different hierarchical structure of the product family, it becomes an abstract Factory mode, and for the abstract factory model, when the reduction of a method of the provided products no longer constitute the product family, It evolved into a factory method model.
Transferred from: http://blog.csdn.net/jason0539
---Factory mode for Java design patterns