Simple factory (Simple Factory)
definition:
Simple factory model is a class creation mode, and it is called a Static Factory Method mode.
Simple factory model is an instance of which product class is created by a factory object.
Character:
Factory role (CREATOR):
The role is the core of the factory method model, which contains the close-related business logic. Factory class creates a product object under the direct call of the client, which often has a specific Java class implementation.
Abstract product role (Product):
The class that serves this role is the parent class of the object created by the factory method mode, or the excuses they share together. Abstract product roles can be implemented with a Java interface or Java abstract class.
Concrete Product:
Any object created by the factory method mode is an instance of this role, and the specific product role is implemented by a specific Java class.
Knowledge points:
When the product forms a complex level structure, the simple factory model still uses a factory class.
Advantages: The level structure of the product class will not be reflected in the factory class, so that the change in the level structure of the product is not affected by the factory class.
Disadvantages: Increasing new products will cause the factory class to modify.
In the MVC architecture mode, you can use simple factory models.
Factory roles are controllers, and the product is a view (Model) that can act as the client of this creation process.
Advantages of simple factory model:
The client can exempt the responsibility of directly creating product objects, but only responsible for "consumption" products. The simple factory has achieved segmentation of responsibility.
Disadvantages of simple factory model:
Factory classes cannot represent complex multi-level structures.
The factory class concentrates all the product creation logic to form an omnipotent all-round class, God class. This God class is responsible for all products. If there is a problem, it will affect all product creation processes.
When the product class has a different direct interface type, the factory class needs to be judged when creating a product. This determination of the timing and the judgment logic of which specific product are mixed, so that the system is difficult to expand in the future. This disadvantage is overcome in the factory method mode.
Since the simple factory model uses a static method as a factory method, the static method cannot be inherited by subclasses, so the factory character cannot form a level structure based on inheritance. This disadvantage is overcome in the factory method mode.
Simple factory models are only supported "open-closed" principles in limited extent. The client can accept new products without modifying, but factory roles can only be modified.
Example application:
1. An example of a farm fruit.
Abstract product class:
Public Interface Fruit, INTERFAIT
{
Void growth ();
Void Harvest ();
Void Plant ();
}
Specific product class:
Public Class Grape Implements Fruit
{
Public void greow ()
{
System.out.println ("Grape is glout ...");
}
Public void harvest ()
{
System.out.Println ("Grape Has Been Harvested.");
}
Public void plant ()
{
System.out.println ("Grape Has Been Plate);
}
Public boolean getseedless ()
{
Return seedless;
}
Public void setseedless (Boolean SEEDLESS)
{
THIS.SEEDLESS = SEEDLESS;
}
PRIVATE BOOLEAN SEEDLESS;
Factory class:
Public Class Fruitgardener
{
Public Static Fruit Factory (String Which) THROWS BADFRUITEXCEPTION
{
IF (Which.equalsignoreCase ("apple"))
{
Return new apple ();
}
Else IF (Which.equalsignorecase ("strawberry"))
{
Return new strawberry ();
}
Else IF (Which.equalsignorecase ("grape"))
{
Return new grape ();
}
Else
{
Throw New Badfruitexception ("Bad Fruit Request");
}
}
}
Client:
Try {
FRUITGARDERNER.FACTORY ("grape");
Fruitgarderner.Factory ("apple");
FRUITGARDERNER.FACTORY ("strawberry");
}
Catch (Badfruitexception E) {
}
2. Female
Factory class: female
Abstract product: abstract people concept
Specific product: Specific person, Zhang San, Li Si.