1. Simple Factory mode (Factory pattern) 1.1. Mode motive
Consider a simple software scenario in which a software system can provide multiple different-looking buttons (such as round buttons, rectangle buttons, diamond buttons, and so on) that originate from the same base class, but after inheriting the base class, different subclasses modify some of the properties so that they can render different appearances. If we want to use these buttons, we do not need to know the names of these specific button classes, just need to know a parameter that represents the button class, and provide a convenient way to call, pass the parameter to the method to return a corresponding button object, at this time, you can use the Simple Factory mode.
1.2. Pattern definition
Simple Factory pattern: Also known as the Static Factory method (Factory mode), it belongs to the class-creation pattern. In the simple Factory mode, you can return instances of different classes depending on the parameters. The simple factory model specifically defines a class to be responsible for creating instances of other classes, and the instances that are created typically have a common parent class.
1.3. Pattern structure
The Simple factory model contains the following roles:
Product: Abstract Products role
Abstract product roles are the parent of all objects created, and are responsible for describing common interfaces common to all instances
Concreteproduct: Specific Product roles
A specific product role is to create a target, and all created objects act as instances of a specific class for that role.
650) this.width=650; "alt=". /_images/simplefactory.jpg "src=" Http://design-patterns.readthedocs.org/zh_CN/latest/_images/SimpleFactory.jpg " />
1.4. Timing Diagram
650) this.width=650; "alt=". /_images/seq_simplefactory.jpg "src=" http://design-patterns.readthedocs.org/zh_CN/latest/_images/seq_ Simplefactory.jpg "/>
1.5. Code Analysis
#include "Factory.h"
#include "ConcreteProductA.h"
#include "ConcreteProductB.h"
Product * Factory :: createproduct (string proname) {
if ( "A" = = proname )
{
return New concreteproducta ();
}
Else if ("B" = = proname) {
return New CONCRETEPRODUCTB ();
}
return NULL ;
}
1.6. Pattern Analysis
Separating object creation from the business process of the object itself can reduce the coupling of the system, making both of them relatively easy to modify.
When calling the factory method of plant class, because the factory method is a static method, it is convenient to use, can be called directly through the class name, and only need to pass in a simple parameter, in the actual development, you can also save the parameters passed in the XML format in the configuration file. There is no need to modify any source code when modifying parameters.
The biggest problem with the simple factory model is that the responsibilities of the factory class are relatively heavy, and adding new products requires modifying the judgment logic of the factory class, which is contrary to the open and closed principle.
The point of the simple factory model is that when you need something, just pass in the correct parameter, and you get the object you want without having to know its creation details.
1.7. Advantages of Simple Factory mode
The factory class contains the necessary judgment logic to decide when to create an instance of the product class, and the client can dispense with the responsibility to create the product object directly, rather than simply "consume" the product; The simple factory model implements the separation of responsibilities by this approach, which provides a specialized factory class for creating objects.
The client does not need to know the class name of the specific product class created, only need to know the corresponding parameters of the specific product class, for some complex class names, through the simple factory model can reduce the user's memory capacity.
By introducing a configuration file, you can change and add new product classes without modifying any client code, to some extent, improve the flexibility of the system.
1.8. Disadvantages of the Simple factory model
As the factory class centralizes all product creation logic, the entire system is affected once it does not work properly.
Using the simple Factory mode will increase the number of classes in the system, adding complexity and difficulty to the system in certain programs.
System expansion is difficult, once added new products will have to modify the factory logic, in the product type is more likely to cause the factory logic is too complex, not conducive to the expansion and maintenance of the system.
Simple Factory mode due to the use of the static factory method, the factory role cannot form an inheritance-based hierarchy.
1.9. Applicable environment
You can use the Simple Factory mode in the following situations:
The factory class is responsible for creating fewer objects: because fewer objects are created, it does not cause the business logic in the factory method to be too complex.
The client knows only the parameters of the incoming factory class, and does not care about how to create the object: The client does not need to care about the creation details, even the class names do not need to be remembered, just need to know the parameters of the type.
1.10. Mode application
The JDK class library is widely used in simple Factory mode, such as Tool class Java.text.DateFormat, which is used to format a local date or time.
Public final static DateFormat getdateinstance ();p ublic final static dateformat getdateinstance (int style);p Ublic final S tatic dateformat getdateinstance (int style,localelocale);
Java Encryption Technology
Get key generators for different cryptographic algorithms:
Keygenerator keygen=keygenerator.getinstance ("Desede");
To create a cipher:
Cipher cp=cipher.getinstance ("Desede");
1.11. Summary
The Create pattern abstracts the instantiation of a class, allowing the creation of objects to be separated from the use of objects.
The simple Factory mode, also known as the static factory method pattern, belongs to the class-creation pattern. In the simple Factory mode, you can return instances of different classes depending on the parameters. The simple factory model specifically defines a class to be responsible for creating instances of other classes, and the instances that are created typically have a common parent class.
The Simple factory model contains three roles: the factory role is responsible for implementing the internal logic of creating all instances; The abstract product role is the parent of all objects created, and is responsible for describing the common interfaces common to all instances; The product role is to create the target, and all created objects act as instances of a specific class of this role.
The point of the simple factory model is that when you need something, just pass in the correct parameter, and you get the object you want without having to know its creation details.
The biggest advantage of the simple factory model is that the object creation and the use of the object separation, the creation of the object to the specialized factory class responsible, but its biggest disadvantage is that the factory class is not flexible enough to add new concrete products need to modify the factory class Judgment logic code, and when the product is more, the factory method code will be very complex.
The simple factory model is suitable for cases where the factory class is responsible for creating fewer objects, and the client only knows the parameters of the incoming factory class and does not care about how to create the object.
This article from "Lao Cai's Home" blog, declined to reprint!
Simple Factory mode (easy Factory pattern)