(C #) Factory method mode

Source: Internet
Author: User

1. Factory Method Mode

The first is an interface for creating an object, letting the subclass decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses.

* Factory method mode overcomes the disadvantage of the simple Factory mode violating the open-closed principle, and preserves the advantages of the encapsulation object creation process.

2. Example

namespaceFactory method Mode {classProgram {Static voidMain (string[] args) {Ifactory Factory=Newcundergraduate (); Leifeng Student=Factory.            Createleifeng (); Student.            Sweep (); Student.            Wash (); Student.            Buyrice ();        Console.ReadLine (); }    }    /// <summary>    ///classes of operations performed by public in all factories/// </summary>    classLeifeng { Public voidSweep () {Console.WriteLine ("sweeping"); }         Public voidWash () {Console.WriteLine ("Laundry"); }         Public voidBuyrice () {Console.WriteLine ("Buy Rice"); }    }    /// <summary>    ///Create a factory interface/// </summary>    Interfaceifactory {Leifeng Createleifeng (); }    /// <summary>    ///classes that the factory needs to instantiate/// </summary>    classUndergraduate:leifeng {}/// <summary>    ///classes that the factory needs to instantiate/// </summary>    classVoluteers:leifeng {}/// <summary>    ///factory, used to instantiate classes/// </summary>    classCundergraduate:ifactory { PublicLeifeng Createleifeng () {return NewUndergraduate (); }    }    /// <summary>    ///factory, used to instantiate classes/// </summary>    classCvoluteers:ifactory { PublicLeifeng Createleifeng () {return Newvoluteers (); }    }}

(C #) Factory method mode

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.