1. Simple factory mode
1.1 meaning
- The simple factory pattern is also called the static method pattern (because the factory class defines a static method)
- In real life, factories are responsible for producing products; Similarly, in the design pattern, the simple factory pattern can be understood as a class responsible for production objects, which is called "factory class".
1.2 mode composition
1.3 use steps
- Create abstract product classes (parent class of a specific product class) & defines the public interface of a specific product;
- Create concrete product class (inherit abstract product class) & define the concrete products produced;
- Create factory classes, and create instances of different specific product classes by creating static methods according to different parameters passed in;
- The outside world creates instances of different specific product classes by calling the static methods of the factory class and passing in different parameters
1.4 example introduction
There is an existing fruit processing factory, which needs to process three kinds of fruits (apple, pear and orange), which is realized by using the simple factory mode.
Step 1: create an abstract product class and define the public interface of specific products.
abstract class Product{ public abstract void Show(); }
Step 2 Create a specific product class (inherit the abstract product class) and define the specific products produced.
//Specific product category A class ProductA extends Product{ @Override public void Show() { System.out.println("Processed apple"); } } //Specific product category B class ProductB extends Product{ @Override public void Show() { System.out.println("Processed pear"); } } //Specific product category C class ProductC extends Product{ @Override public void Show() { System.out.println("Processed orange"); } }
Step 3 Create a factory class and create static methods to create instances of different specific product classes according to different parameters.
class Factory { public static Product Manufacture(String ProductName){ //In the factory class, switch statements are used to control which goods are produced; //The user only needs to call the static method of the factory class to instantiate the product class. switch (ProductName){ case "A": return new ProductA(); case "B": return new ProductB(); case "C": return new ProductC(); default: return null; } } }
Step 4 The outside world creates instances of different specific product classes by calling the static methods of the factory class and passing in different parameters.
//Fruit processing process in factory public class SimpleFactoryPattern { public static void main(String[] args){ Factory mFactory = new Factory(); //Customers require apple to be processed try { //Call the static method of the factory class & pass in different parameters to create a product instance mFactory.Manufacture("A").Show(); }catch (NullPointerException e){ System.out.println("The factory can't process the fruit"); } //Customer requests to process pear try { mFactory.Manufacture("B").Show(); }catch (NullPointerException e){ System.out.println("The factory can't process the fruit"); } //Customers require processing orange try { mFactory.Manufacture("C").Show(); }catch (NullPointerException e){ System.out.println("The factory can't process the fruit"); } //The customer asked to process banana try { mFactory.Manufacture("D").Show(); }catch (NullPointerException e){ System.out.println("The factory can't process the fruit"); } } }
Compared with the fact that I want to process apples now, according to the original idea, I directly new a factory for processing apples, but now there is a general factory (which can process three kinds of fruits). I can go directly to this factory to tell my needs and ask him to help me process fruits. We don't care how he processes my fruits, I just throw my needs to him and leave the rest to him to complete. In short, a simple factory changes the things we need to do before. Now we directly face a factory and let it help us complete the things. (in fact, the three factory design modes are all based on this idea, but each has its own advantages and disadvantages)
1.5 advantages of simple factory mode
- The work of creating instances is separated from the work of using instances. Users do not have to care about how to create class objects, which realizes decoupling;
- Put the work of initializing the instance into the factory to make the code easier to maintain. More in line with the principle of object-oriented & Interface oriented programming, rather than implementation oriented programming.
1.6 disadvantages of simple factory mode
- The factory class centralizes the creation logic of all instances (products). Once the factory fails to work normally, the whole system will be affected;
- It does not comply with the "opening and closing principle". Once a new product is added, the logic of the factory class will have to be modified, which will cause the factory logic to be too complex.
- Because static factory methods are used in simple factory mode, static methods cannot be inherited and rewritten, which will make the factory role unable to form an inheritance based hierarchical structure.
1.7 application scenario of simple factory mode
- If the customer only knows the parameters of the incoming factory class and does not care about the logic of how to create the object; (if I only know to process apples, that is, the parameter is A)
- When the factory class is responsible for creating fewer objects (specific products).