C # design mode (3) -- factory method mode

Source: Internet
Author: User

I. Introduction

When talking about the disadvantages of the simple factory model in the simple factory model, one thing is that the system in the simple factory model is difficult to expand. Once a new product is added, You have to modify the simple factory method, in this way, the Implementation logic of the simple factory is too complicated. However, the factory method model introduced in this topic can solve this problem in the simple factory model, the following describes how the factory model solves the problem.

II. Implementation of the factory method model

The factory method mode can solve the simple factory mode because its implementation delays the creation of a specific product to a subclass. At this time, the factory class is no longer responsible for the creation of all products, instead, the interface that must be implemented by a specific factory is provided. In this way, the factory method mode allows the system to add new products without modifying the factory logic, which overcomes the disadvantages of the simple factory mode. Let's take a look at the specific implementation code of the factory mode (Here we use the example of ordering in the simple factory mode ):

                                                                                                                                                                 Main(            Creator shreddedPorkWithPotatoesFactory = =             Food tomatoScrambleEggs =            Food shreddedPorkWithPotatoes =

If the system is implemented using the factory method and a new product needs to be added, we can use polymorphism to expand the system. No changes are required to the Code in the abstract factory class or specific factory. For example, if we want to order an eggplant with minced meat, we only need to defineSpecific factory type of minced eggplantAndMinced eggplantYou can. Instead of modifying the implementation in the factory class as in the simple factory mode (specifically, adding a case statement ). The Code is as follows:

                                                                                    Main(            Creator minceMeatEggplantFactor =             Food minceMeatEggplant =
Iii. UML diagram of factory method mode

After explaining the specific implementation of the factory model, let's look at the UML diagrams between various types in the factory model:

From the UML diagram, we can see that,. The UML diagram is explained as follows:

: Act as an abstract factory. Any specific factory must inherit this abstract class.

: Act as a specific factory role to create a specific product

: The abstract class of a specific product. Any specific product should inherit this class

: Act as the role of a specific product to implement the abstract methods defined by the abstract product class, created by the specific factory class, there is a one-to-one relationship between them.

Iv.. NET implements the factory method class

. The NET Class Library also has many classes that implement factory methods. For example, in Asp.net, the processing program object is used to process requests. When we request *. when the aspx file is created, it is mapped to System. web. UI. pageHandlerFactory*. Ashx requests are mapped to System. Web. UI. SimpleHandlerFactory.(Both of these classes inherit from the IHttpHandlerFactory interface. NET \ Framework \ v4.0.30319 \ Config \ Web. the Config file contains the following definitions:

 

The following describes how the factory method mode is implemented in Asp.net. If a request is sent to an Index. aspx pagePageHandlerFactoryCreate an Index. aspx object using the GetHandler method in. The class graph relationships between them are as follows:

V. Summary

The factory method mode delays object creation to a specific factory through polymorphism in Object-Oriented Programming, which solves the problems existing in the simple factory mode, it also complies with the open and closed principle (that is, the extension development and the modification and sealing ).

Related Article

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.