C # Design pattern (3)--factory method Mode (Factory methods)

Source: Internet
Author: User

Get different objects through factory factory in simple Factory mode, but with one obvious drawback-simple Factory mode systems are hard to scale!

Once you add a new product, you have to modify the simple factory method, which makes the implementation logic of the simple factory too complex,

This problem can be solved by the factory method pattern in simple Factory mode, see Code:

    /// <summary>    ///Automotive Abstract class/// </summary>     Public Abstract classAuthcar {/// <summary>        ///What kind of car does the output make?/// </summary>         Public Abstract voidCreatorcar (); }    /// <summary>    ///derived class-orange sedan/// </summary>     Public class_oriangecar:authcar { Public Override voidCreatorcar () {Console.WriteLine ("production of orange sedan!"); }    }    /// <summary>    ///derived class-black sedan/// </summary>     Public class_blackcar:authcar { Public Override voidCreatorcar () {Console.WriteLine ("produced a black sedan"); }    }

There are two different colors of cars (orange and black) under the car category, and the following are different cars from the factory abstract class.

    /// <summary>    ///Factory class/// </summary>     Public Abstract classFactorycreator {/// <summary>        ///Get Car Objects/// </summary>        /// <returns></returns>         Public AbstractAuthcar _authcar (); }    /// <summary>    ///get examples of black cars/// </summary>     Public classBlackcarfactorymethod:factorycreator { Public OverrideAuthcar _authcar () {return New_blackcar (); }    }    /// <summary>    ///Get orange car examples/// </summary>     Public classOriangefactorymethod:factorycreator { Public OverrideAuthcar _authcar () {return New_oriangecar (); }    }

Test

class program    {        staticvoid Main (string[] args)        {            new  Blackcarfactorymethod ();             New Oriangefactorymethod ();            Factorycreator._authcar (). Creatorcar ();            Console.readkey ();        }    }

Systems implemented using the factory method, if the system needs to add new products, we can use polymorphism to complete the expansion of the system, there is no need to make any changes to the abstract factory class and the code in the specific factory.

For example, we also want to export a green car, at this point we just need to define a green car specific factory class and Green car class can be. Instead of modifying the implementation in the factory class as in simple Factory mode (specifically, adding case statements).

C # Design pattern (3)--factory method Mode (Factory methods)

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.