This is the 25th day of my participation in Gwen Challenge

background

I was asked a question yesterday, why use the factory model? There is no need to add a factory class to create objects. Isn’t it more convenient to create new objects directly? Answer this question as you understand it today.

Why use factory mode

There are three pieces of responsibility associated with an object:

  • Responsibilities of objects themselves: The functions provided by objects themselves, that is, the business functions provided by object classes
  • Object creation responsibilities: Responsible for creating objects
  • Responsibilities for using objects: Responsible for using objects

In simple use, we use new() to create an object and place it next to the object, for example:

String str = new String("test");
str.func();
Copy the code

Used as String () this simple class so is no problem, can create and use in one place, but for some complex business class, create the object code to be complicated (it can be conceived as to do a lot of operation), from the standpoint of code maintenance and code reuse, so, if you still put the logic of creating and using objects together, then the code It gets harder and harder to maintain

So the factory pattern is used at this point. Its core point is to separate the logic of creating objects and put it in the factory to complete, so that the three parts of the object’s responsibility is completely isolated and can be maintained independently

The introduction of the factory pattern will reduce the maintenance effort caused by product or factory class changes:

  • If the logic for creating an object changes, such as the constructor, then you only need to change the code in the factory without changing where the object is used
  • If the interface methods provided by the object change, such as adding or removing interfaces, then you only need to change where the object is used, not the factory methods

The factory pattern reduces code duplication. If the process of creating object B is complicated, requires a certain amount of code, and is used in many places, then there will be a lot of duplicate code. We can put the code that created object B in a factory and manage it all together. It not only reduces duplicate code, but also facilitates modification and maintenance of the creation process of B in the future.

A lot of data in the code are simply demonstrates how to write a factory pattern, but didn’t write the key points, in the factory class is simply a new () to generate the object after the return to go out, look from the surface, so the factory pattern is unnecessary, as the new () for creating an object directly to the object and use convenient place together

So to understand the need for the factory model, think more complicated:

  • The logic for creating objects is complex and changes frequently. Without the factory pattern, every place where an object is used has to be written, and once a logical change is made, everything has to be changed
  • Once you use the factory pattern, you can put the complex logic of creating objects in the factory method

In all factory patterns, it is emphasized that the relationship between two classes A and B should only be one where A creates B or where A uses B, not both. The separation of object creation and use makes the system more consistent with the “single responsibility principle”, which is conducive to function reuse and system maintenance.

Another advantage of using a factory class is, if you don’t use the factory pattern, a class may have more than one constructor, the client can only by passing in different parameters to invoke the constructor to create objects, from the constructor parameter list and perhaps users don’t understand the differences between the constructor for the product structure. But if the object creation process packaging in the factory, we can provide a series of name completely different factory method, each factory method corresponds to a constructor, the client can in a more readable, easy way to create objects, and, from a set of factory method selecting a meaning clear factory method, Much more convenient than choosing a constructor from a set of constructors with the same name and different parameters.