The simple factory mode is the class creation mode, also called the static factory method mode.
Several forms of the factory model of 12.1 are as follows:
① Simple factory, also known as the static factory method mode;
② Factory method, also known as polymorphic factory mode or virual constructor mode;
③ Abstract Factory, also known as Toolkit (kit or Toolkit) mode.
☆Simple factory mode is a special implementation of different factory method modes. In Java, the common factory method mode cannot provide a static factory mode by degrading the design function.
12.2 introduction of simple factory Model
This section introduces a simple factory model through a farm instance. The farm provides three types of fruits: Grape, strawberry, and apple. There is also a gardener, fruitgardener, responsible for providing the fruit customers need. Their UML diagram is as follows:
The fruit interface defines the abstraction of all fruits, including planting, growing, and harvesting. The Code is as follows:
Public interface fruit
...{
Public void grow ();
Public void harvest ();
Public void plant ();
}
Apple is a fruit, and because it is a perennial plant, there is a treeage attribute, the Code is as follows:
Public class Apple implements fruit
...{
Public void grow ()
...{
System. Out. println ("Apple: Grow ");
}
Public void harvest ()
...{
System. Out. println ("Apple: havest ");
}
Public void plant ()
...{
System. Out. println ("Apple: Plant ");
}
Private int treeage;
Public int gettreeage ()
...{
Return this. treeage;
}
Public void settreeage (INT treeage)
...{
This. treeage = treeage;
}
}
Grape is also a fruit with the seedles attribute. The Code is as follows:
Public class grape implements fruit
...{
Private Boolean seedless;
Public void grow ()
...{
System. Out. println ("Grape: Grow ");
}
Public void harvest ()
...{
System. Out. println ("Grape: havest ");
}
Public void plant ()
...{
System. Out. println ("Grape: Plant ");
}
Public Boolean getseedless ()
...{
Return seedless;
}
Public void setseedless (Boolean seedless)
...{
This. Seedless = seedless;
}
}
The code for strawberry is as follows:
Public class strawberry implements fruit
...{
Public void grow ()
...{
System. Out. println ("Strawberry: Grow ");
}
Public void harvest ()
...{
System. Out. println ("Strawberry: havest ");
}
Public void plant ()
...{
System. Out. println ("Strawberry: Plant ");
}
}
The gardener fruitgardener is a factory-type role in the farm. It produces different fruits based on the customer's needs (different input parameters. The Code is as follows:
Public class fruitgardener
...{
Public static fruit getfruit (string name)
...{
Fruit result = NULL;
If (name. inclusignorecase ("apple "))
Result = new Apple ();
Else if (name. inclusignorecase ("grape "))
Result = new grape ();
Else if (name. inclusignorecase ("strawberry "))
Result = new strawberry ();
Return result;
}
}
12.3 structure of simple factory Model
☆The simple factory model involves three roles:
① Factory role: this role is at the core of the factory method model and contains business logic closely related to applications. The factory class creates a product object under direct calls from the client, which is often implemented by a specific Java class.
② Abstract Product role: the class that assumes this role is the parent class of the object created by the factory method mode, or the interfaces they share. Abstract product roles can be implemented using a Java interface or Java Abstract class.
③ Specific product role: Any object created in the factory method mode is an instance of this role. The specific product role is implemented by a specific Java class.
12.4 Implementation of simple factory Model
☆For a multi-level product structure, a factory class is used in a simple factory mode. The advantage is that the design is simple, but the disadvantage is that new products will inevitably cause changes to the factory class.
☆A factory class can have multiple factory methods to return different product roles
☆Omitting roles in simple factory methods
① Omitting the abstract Product role. If there is only one specific product in the system, the abstract Product role can be omitted;
② Merge factory roles with abstract product roles, for example, java. Text. dateformat;
③ Merge all three roles.
12.5 relationship between simple factory model and other models
Singleton and Singleton modes are special applications of simple factory methods. Others include memorandum mode and more advanced MVC mode.
Advantages and disadvantages of simple factory model 12.6
☆Advantage: the core of this mode is the factory class. This class contains the necessary judgment logic to determine when to create a product class instance. While the client can avoid the responsibility of directly creating product objects, and only consume products. In simple factory mode, responsibility is separated through this method.
☆Disadvantages: The factory class bears too much logic and forms an all-powerful God class. The factory in simple factory mode is static implementation and cannot be inherited. The factory class can only partially meet the principle of opening and closing.