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.
Factory Method Mode
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
Abstract Factory mode
The abstract factory model is actually part of the abstraction of the factory method model. In factory method mode, the two ways to send a message are to match in the class factory and then invoke the relevant specific factory execution, and if you need to add another way to send the message, you need to expose the class factory to the programmer, which is very dangerous. Therefore, the method of invoking the specific factory should be abstracted out, so that each factory has its own calling method, abstract create this interface, each invocation method inherits this interface, when the need to increase or decrease the mode of transmission, only to throw this interface, to achieve a low coupling, improve security.
The code is as follows:
Package Factory_method;
public class abstractfactory{
/**
* Define public interface Send
*/
Interface Send {
void Send ();
}
/**
* Define specific factory roles and inherit abstract factory roles
*/
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 message by e-mail");
}
}
/**
* Creating a new interface create, invoking the specific factory in every way has its own class
*
*/
Interface create{
void method ();
}
Class Smsmethod implements create{
@Override
public void Method () {
TODO auto-generated method stubs
New SMSfactory (). Send ();
}
}
Class Mailmethod implements create{
@Override
public void Method () {
TODO auto-generated method stubs
New Mailfactory (). Send ();
}
}
/**
* Define User
*/
public static void Main (String args[]) {
Abstractfactory temp=new abstractfactory ();
Temp.new Smsmethod (). method ();
Temp.new Mailmethod (). method ();
}
}
Summarize
Factory mode and abstract factory model are all part of the factory model, the ultimate goal is to reduce the coupling degree, do not have to be too tangled in which mode, in the case of conditions permitting, can abstract common interface as far as possible to abstract out, can be more convenient maintenance and extension programs.
Java Design pattern (creation: Factory method mode + Abstract Factory mode)