(The content of the original, there are shortcomings in the hope of more advice!) )
Design patterns can be divided into three categories according to the objective criteria:
Create: Creational The design pattern of the created type is related to the creation of the object.
Structured: Structural handles the combination of classes and objects.
Behavioral: Behavioral Describes how classes and objects interact and assigns responsibilities.
What is the factory method model?
At the user's point of view, the factory method pattern refers to the requirement that the user proposes specific functions to be implemented, or specific things that they want to produce, and delivers the thing or function to the user after it has been manufactured through the factory. The user does not know the implementation of the specific process, only to do the request and the implementation of the results of the use.
Strictly speaking, the Factory mode (Factory method) is also called the Factory pattern, which belongs to the class creation pattern, the Factory parent Class (interface) is responsible for defining the public interface of the product object, and the subclass factory is responsible for creating the specific Product object.
Objective: To defer the instantiation of the product to the subclass factory, and to determine which product specific object should be instantiated by the factory subclass.
There are four components:
1. Abstract Factory role: the interface that a specific factory must implement or the parent class that must inherit. Typically implemented by an abstract class or interface.
2. Specific factory role: is the specific application called to create the corresponding specific product object. Generally it is implemented by specific classes.
3. Abstract Product Role: The parent class of a specific product inheritance or an implemented interface. Typically implemented by an abstract class or interface.
4. Specific product role: the object created by the specific factory role is its instance. Typically implemented by specific classes.
The four parts and application of the factory method pattern are distinguished by the example.
Example: Boss proposes to send a message to employees
Public class factorymethod{
/**
* Define the Common interface send
*/
Interface Send {
void Send ();
}
/**
* Define specific factory roles and inherit public interfaces
*/
class SMSfactory implements send{
@Override
Public void Send () {
TODO Auto-generated method stubs
System. out. println ("Send message via SMS");
}
}
class Mailfactory implements send{
@Override
Public void Send () {
TODO Auto-generated method stubs
System. out. println ("Send messages by e-mail");
}
}
/**
* Match the method specified by the user with the sub-factory, using the factory to achieve
*
*/
class factory{
Public void Method (String a) {
if ("SMS". Equals (a)) {new smsfactory (). Send ();}
Else if ("Email". Equals (a)) {new mailfactory (). Send ();}
Else
System. out. Print ("input error");
}
}
/**
* Define User
*/
Public Static void Main (String args[]) {
FactoryMethod temp=New FactoryMethod ();
Factory temp1=temp. New Factory ();
Temp1.method ("SMS");
Temp1.method ("email");
}
}
Output: Send message via SMS
Send a message by e-mail
Java Design pattern (creation: Factory method Mode)