Critical factory method model

Source: Internet
Author: User

The reason for taking this title is purely to attract attention ,:)

I have never used patterns and have no practical experience. Therefore, I would like to ask a special question.
In the factory method mode, there are two types of systems, one is the product class system, such as iproduct, concreteproducta, concreteproductb, concreteproductc, the specific product class all implement iproduct interface. The other is the factory system, such as ifacloud, concretefactorya, concretefactoryb, and concretefactoryc. The corresponding factory class is used for the specific product class corresponding to "production.

We generally write the following statement in use:
Ifactory F = new concretefactorya ();
Iproduct P = f. createproduct ();
P. executefunction (); // customerCodeUse the functions provided by iproduct.

If a new product is available, we will first create a new product and its corresponding factory class, and then modify the Code as follows:
Ifactory F = new concretefactoryd (); // change this location only
Iproduct P = f. createproduct ();
P. executefunction ();

Although the client code is no longer dependent on specific product classes, it is dependent on specific factory classes.

If we do not use the factory method mode, write the code as follows:
Iproduct P = concreteproducta ();
P. executefunction ();
Obviously, a line of code is missing without the factory method mode. What are the benefits of the factory method model? Is it because I write more code?

If the client code does not depend on the specific product class or factory class, but on the abstract product class or the abstract factory class, the Customer Code does not need to be modified when the specific product class is changed. If the language platform provides the reflection function, you can change the customer code to the configuration file:
Ifactory F = (ifacloud) Assembly. Load ("assemblynamefromconfig"). createinstance ("concretefactorya ");
Iproduct P = f. createproduct ();
P. executefunction (); // functions provided by iproduct are used in customer code.

If we do not use the factory method mode, write the code as follows:
Iproduct P = (iproduct) Assembly. Load ("assemblynamefromconfig"). createinstance ("concretefactorya ");
P. executefunction (); // is this method Provider Model?
Obviously, I wrote a line of code using the factory method mode. Could this be the benefit of the factory method mode? Is it based on abstract programming? I feel that I have not found a real scenario for using the factory method. Please help me find it.

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.