The Factory mode is responsible for instantiating a large number of common interfaces, and the Factory mode can dynamically determine which class instantiation. Factory mode can be divided into three:
1) Simple Factory Method mode.
2) Factory Methic Factory Polymorphic Factory Modal or (Virtural Constructor) Virtual Configuration Submatings.
3) Abstractract Factory mode (Kit / Toolkit) toolbox mode.
Simple factory (Simple Factory)
Simple factory model is a class creation mode, called Static Factory Method mode,
SimpleFactory is an instance of the product class by a factory object.
Simple factory's mythical instance is a female people.
Simple Factory mode involves three roles: factory roles, abstract product roles, specific product roles.
Factory role: (CREATOR)
Contains business logic related to specific applications. Factory class creates product objects under direct calls of the client.
Abstract product role: (Product)
The class that serves this role is the parent class of the factory, or they have a common interface.
Specific product role: (Concrete Product)
Any object created by the factory method is an example of this role.
The application of Simple Factory in Java is Java.Text.DateFormat, etc.
Disadvantages: No scalability. It is not enough to support the "open-closed" principle.
Factory Method (Factory Method)
Use polymorphism and retains the advantages of Simple Factory. Overcomes the shortcomings.
In Factory Method, the core factory class is no longer responsible for the creation of all products, and the specific creation work is handed over to subclasses and become the role of abstract factories. The interface that must be implemented only for the specific factory subclass.
The system of factory method patterns involves the following roles:
Abstract Factory: Any factory class that creates an object in the mode must implement this interface. This class often has an abstract Java class to achieve.
Concrete Creator: A specific class of abstract factory interfaces, closely related to the application system, and is called by the application to create product objects.
Abstract Products: The super-type of the object created by the factory method mode, that is, the common parent class or common interface of the product object. In the actual system, there is an abstract Java class to implement.
Concrete Product: The interface declared by an abstract product role is implemented, and each object created by the factory method mode is an example of a specific product role.
If the system adds a new product, only the class of this product and his factory class, there is no need to modify other classes, so that the "open-closed" principle is fully supported.
Abstract factory roles and abstract product characters can be implemented with Java interfaces and abstractions.
If the specific factory character has a common logic, then these common logic can move up to the abstract factory role, which means that abstract factory roles should have a Java abstraction class to achieve. And provide a default method by this abstract factory.
Factory methods do not necessarily return a new object each time, but each object must be built by himself, rather than outside the object of the object. The factory method returns an abstract type, not a specific type, ensuring the polymorphism of the product.
Applications in the Java class library:
1, the overall of multiple objects aggregated together is called aggregation. The aggregation object is a container object that can accommodate a set of objects. For example, Vector ArrayList, all implement the Java.util.Collection interface, which specifies all of the aggregates to provide an item () method, returning an object of the Iterator type.
Iterator () is a factory method.
Abstract factory mode (Abstract factory)
The biggest difference between abstract factory model and factory method model is that the plant method mode is a structure of a product level, and abstract factory models need to face multiple product rating.
Abstract plants in the factory |:
Abstract Factory Role: This role is the core of the factory method mode. He is unrelated to the business logic of the application system, usually using the Java interface or abstract class. And all specific factory classes must implement this Java interface or inheritance. This abstract class.
Specific Factory (ConreFactory) Role: This role is directly creating an instance of the product under the client. This role contains the logic of the selected product object. This logic is specific to the business logic of the application system.
Abstract Product (AbstractProduct) Role: A class that serves this role is the parent class of the object of the factory method, or they have a common interface, which is usually a Java interface or abstract class.
ConveProduct Role: Any product object created by the abstract factory mode is an instance of a specific product class. This is the final thing of the client. It must be full of business logic of the application system.