Factory mode, factory mode java
- Factory model classification
Simple factory ModeIs the Creation Mode of the class, also known as the static factory method mode. In simple factory mode, a factory class determines which product class to create based on input parameters.
Factory method modeIt is derived from the simple factory model and solves many problems of the simple factory model. The factory method mode abstracts the simple factory mode. There is an abstract Factory class (which can be an abstract class and an interface). This class is not responsible for specific product production, but only for some specifications. The specific production work is completed by its subclass. In this mode, the factory class and product class can usually correspond in sequence. That is, an abstract factory corresponds to an abstract product, and a specific factory corresponds to a specific product, which is responsible for producing the corresponding product.
Abstract Factory ModelIt is the most abstract and general form of the factory model in all forms. Abstract Factory mode refers to a factory mode used when multiple abstract roles exist. Abstract Factory mode provides an interface to the client to create product objects in multiple product families without specifying the specific product. According to the LSP principle, any place that accepts the parent type should be able to accept the child type. Therefore, what the system actually needs is only some instances with the same type as those of abstract products, rather than the instances of these abstract products. In other words, it is an example of the concrete sub-classes of these abstract products. The factory class is responsible for creating instances of concrete subclasses of abstract products.
- Application of the factory Model
Application Scenario: This section uses a common example to describe the application of various factory models. Generally, there are various fruits in the orchard. Each type of fruit is equipped with a gardener.
Simple factory mode: At first, the orchards were not large, and all fruits were managed by a gardener.
Abstract product Fruit:
1 public interface Fruit {2 void fruitType();3 }
Specific products:
1 public class Apple implements Fruit {2 @ Override 3 public void fruitType () {4 System. out. println ("this is Apple"); 5} 6 7} 8 public class Grape implements Fruit {9 @ Override10 public void fruitType () {11 System. out. println ("this is grape"); 12} 13}
Factory:
1 public class FruitFactory { 2 public static Fruit factory(String fruit){ 3 if("apple".equalsIgnoreCase(fruit)){ 4 return new Apple(); 5 }else if("grape".equalsIgnoreCase(fruit)){ 6 return new Grape(); 7 }else{ 8 return null; 9 }10 }11 }
Advantages of the simple factory model: the factory implements the business logic to determine the product creation time, and the client only needs to provide the product to the factory, and then wait for the factory to return for use.
Disadvantages of the simple factory model: put all the business logic in one factory method, the entire system will crash when this factory method cannot be used, and if you need to expand new products, the factory method needs to be modified again, with poor scalability.
Factory method mode: With the expansion of orchards, it takes a lot of time for a gardener to manage, So multiple gardeners are introduced.
Product interfaces and product implementations are as follows:
Factory interface:
1 public interface Fruit {2 void fruitType();3 }
Specific interface:
1 public class AppleFactory implements Factory{ 2 @Override 3 public Fruit createFactory() { 4 // TODO Auto-generated method stub 5 return new Apple(); 6 } 7 } 8 public class GrapeFactory implements Factory{ 9 @Override10 public Fruit createFactory() {11 // TODO Auto-generated method stub12 return new Grape();13 }14 }
We can see that the factory method mode is the promotion of the simple factory mode. For the newly added product type, you only need to integrate the factory interface, continue to allocate the corresponding gardener or modify the existing factory implementation.
Abstract Factory model: When the orchard scale is expanded, Apple and grape species are also expanded to generate a product family, which requires more detailed management.
Abstract Factory:
1 public interface Factory1 {2 Fruit createFactory1();3 Fruit createFactory2();4 }
Abstract Factory mode implementation:
1 public class AppleFactory1 implements Factory1{ 2 @Override 3 public Fruit createFactory1() { 4 // TODO Auto-generated method stub 5 return new Apple1(); 6 } 7 @Override 8 public Fruit createFactory2() { 9 // TODO Auto-generated method stub10 return new Apple2();11 }12 13 }
Abstract Factory mode advantages: Abstract Factory mode not only has the advantages of factory method mode, but also has the primary advantage of being able to constrain the product family within the class. The so-called product family generally has a certain degree of Association. The abstract factory model can define and describe the association between product families within the class, instead of introducing a new class for management.
Disadvantages of the abstract factory model: Product Family expansion will be very laborious. If a new product needs to be added to the product family, almost all factory classes need to be modified. Therefore, the classification of product hierarchy is very important when abstract factory models are used.
Both the simple factory model, the factory method model, and the abstract factory model belong to the factory model. The form and characteristics are extremely similar. Their ultimate goal is to decouple them. In use, we don't have to worry about whether this mode is factory method mode or abstract factory mode, because the evolution between them is often unpredictable. You will often find that the factory method mode is clearly used. When a new requirement comes and a slight modification is made, after a new method is added, because the products in the class constitute the product family with different levels of structure, it becomes the abstract factory model. For the abstract factory model, when a method is reduced to make the provided product no longer constitute a product family, it becomes the factory method model.
Therefore, when using the factory model, you only need to care about whether the goal of reducing the coupling degree has been achieved.