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

Source: Internet
Author: User

C # design mode (3) -- factory method mode,
Original article address: http://www.cnblogs.com/zhili/p/factorymethod.html一、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 ):

Namespace Design Pattern factory method pattern {// <summary> // course abstract class /// </summary> public abstract class Food {// The output menu public abstract void Print ();} /// <summary> /// tomato scrambled eggs this dish /// </summary> public class TomatoScrambledEggs: Food {public override void Print () {Console. writeLine ("tomato scrambled eggs are ready! ") ;}/// <Summary> // potato shredded meat this dish /// </summary> public class ShreddedPorkWithPotatoes: Food {public override void Print () {Console. writeLine ("potato shredded pork ");}} /// <summary> /// abstract factory class /// </summary> public abstract class Creator {// factory method public abstract Food CreateFoddFactory ();} /// <summary> /// tomato scrambled eggs factory class /// </summary> public class TomatoScrambledEggsFactory: creator {/// <summary> /// create the tomato and scrambled eggs dish // </summary> /// <returns> </returns> public override Food CreateFoddFactory () {return new TomatoScrambledEggs () ;}/// <summary> /// potato shredded meat factory class /// </summary> public class ShreddedPorkWithPotatoesFactory: creator {// <summary> /// create the potato shredded meat dish // </summary> /// <returns> </returns> public override Food CreateFoddFactory () {return new ShreddedPorkWithPotatoes () ;}/// <summary> // Client call /// </summary> class Client {static void Main (string [] args) {// initialize two factories for cooking () Creator recipe = new recipe (); Creator tomatoScrambledEggsFactory = new TomatoScrambledEggsFactory (); // start cooking tomato scrambled eggs Food tomatoScrambleEggs = workshop. createFoddFactory (); tomatoScrambleEggs. print (); // start potato shredded Food shreddedPorkWithPotatoes = shreddedPorkWithPotatoesFactory. createFoddFactory (); shreddedPorkWithPotatoes. print (); Console. read ();}}}

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:

/// <Summary> /// eggplant with minced meat /// </summary> public class MincedMeatEggplant: food {// <summary> /// rewrite the method in the abstract class /// </summary> public override void Print () {Console. writeLine ("minced Eggplant") ;}/// <summary> /// factory type of minced eggplant, create a minced eggplant dish // </summary> public class MincedMeatEggplantFactory: creator {/// <summary> /// create the minced eggplant dish // </summary> /// <returns> </returns> public override Food CreateFoddFactory () {return new MincedMeatEggplant () ;}/// <summary> // Client call /// </summary> class Client {static void Main (string [] args) {// if the customer wants to order the meat and eggplant again // initialize another meat and eggplant factory Creator minceMeatEggplantFactor = new MincedMeatEggplantFactory (); // use the minced meat eggplant factory to create the minced meat eggplant dish Food minceMeatEggplant = minceMeatEggplantFactor. createFoddFactory (); minceMeatEggplant. print (); Console. read ();}}
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,In the factory method model, the factory class and the specific product class have a parallel hierarchical structure, and they correspond one to one. The UML diagram is explained as follows:

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

TomatoScrambledEggsFactory and ShreddedPorkWithPotatoesFactory: used to create a specific product.

Food: The abstract class that acts as an abstract product. Any specific product should inherit this class

TomatoScrambledEggs and ShreddedPorkWithPotatoes: act as the role of a specific product to implement abstract methods defined by abstract product classes. They are created by specific factory classes and have 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 mappedSystem. Web. UI. PageHandlerFactoryClass, and *. ashx requests are mappedSystem. 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.