C # Design pattern (3)--factory method mode

Source: Internet
Author: User

Concept: define an interface for creating objects, so that subclasses decide which class to instantiate, and the factory method defers the instantiation of a class to its subclasses.

Second, the Code implementation

namespaceFactory method Pattern of design pattern {/// <summary>    ///Vegetable Abstract class/// </summary>     Public Abstract classFood {//What's the point of the output?         Public Abstract voidPrint (); }    /// <summary>    ///tomato scrambled egg this dish/// </summary>     Public classTomatoscrambledeggs:food { Public Override voidPrint () {Console.WriteLine ("scrambled eggs with tomatoes, please! "); }    }    /// <summary>    ///potato, shredded pork, this dish ./// </summary>     Public classShreddedporkwithpotatoes:food { Public Override voidPrint () {Console.WriteLine ("potatoes, shredded pork."); }    }    /// <summary>    ///Abstract Factory class/// </summary>     Public Abstract classCreator {//Factory Method         Public AbstractFood createfoddfactory (); }    /// <summary>    ///Tomato Scrambled Egg factory class/// </summary>     Public classTomatoscrambledeggsfactory:creator {/// <summary>        ///is responsible for creating tomato scrambled egg this course/// </summary>        /// <returns></returns>         Public OverrideFood createfoddfactory () {return NewTomatoscrambledeggs (); }    }    /// <summary>    ///potato and shredded pork factory class/// </summary>     Public classShreddedporkwithpotatoesfactory:creator {/// <summary>        ///responsible for the creation of potato shredded pork this dish/// </summary>        /// <returns></returns>         Public OverrideFood createfoddfactory () {return Newshreddedporkwithpotatoes (); }    }    /// <summary>    ///Client Calls/// </summary>    classClient {Static voidMain (string[] args) {            //initialize two plants for cooking ()Creator shreddedporkwithpotatoesfactory =Newshreddedporkwithpotatoesfactory (); Creator tomatoscrambledeggsfactory=Newtomatoscrambledeggsfactory (); //start making tomato scrambled eggsFood Tomatoscrambleeggs =tomatoscrambledeggsfactory.createfoddfactory ();            Tomatoscrambleeggs.print (); //start making shredded potatoes.Food Shreddedporkwithpotatoes =shreddedporkwithpotatoesfactory.createfoddfactory ();            Shreddedporkwithpotatoes.print ();        Console.read (); }    }  }

Three, the plant model four elements

    • Factory interface. The factory interface is the core of the factory method pattern, which interacts directly with the caller to provide the product. In practical programming, an abstract class is sometimes used as an interface to interact with the caller, essentially the same.
    • Factory implementation. In programming, the factory implementation decides how to instantiate the product, is the way to realize the expansion, how many kinds of products need to have, how many concrete factories need to implement.
    • Product interface. The main purpose of the product interface is to define the product specification, and all product implementations must follow the specification of the product interface definition. Product interface is the most concern of the caller, the product interface definition directly determines the stability of the caller code. Similarly, product interfaces can be replaced with abstract classes, but it is best not to violate the Richter scale substitution principle.
    • Product realization. The specific class that implements the product interface determines the specific behavior of the product in the client.

C # Design pattern (3)--factory method mode

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.