When should I use the factory model?

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.