In one discussion, we talked about this: When do we need to encapsulate classes and restrict them from using the factory mode to create instances.
One reason is that when the class constructor changes (name changes, parameter changes, etc.), we only need to change a function in the factory class. You do not need to search all constructors and modify them. Therefore, in general, we 'd better use factory mode encapsulation.
Is that true?
The factory mode is not required. Search & replace is required when the name changes. However, is a pattern trained to solve this problem ?!! It seems like a cow is killing a chicken.
What happens when the parameters change?
The Transfer Parameters of the function have changed.Code?!!
Let's take a look at the definition.
---------------------------
Factory method is a creation mode. It defines an interface for creating objects, but lets subclasses decide which class to instantiate. factory is used when a class cannot predict the type of object to be created or a class needs to be specified by a subclass.
---------------------------
Obviously, the above statement is not true.
As stated in the definition, the factory mode is used only when an instance cannot be created. That is, we plan to use it when creating different instances under different conditions.
A typical example is:
Logger: records data that may be recorded on a local hard disk, system events, remote servers, etc. You can choose where to record logs.
Another example:
Database types: the access may be SQL Server or Oracle. You can choose to create a database to access different databases.