01-Simple Factory

Source: Internet
Author: User

Description: Encapsulates a base class that inherits a base class and can be implemented by a factory class when a different object needs to be instantiated. Example: implemented by a calculator applet. The code is implemented as follows: Operation base class
    /// <summary>    ///Operation class/// </summary>     Public classoperaction {Private Double_numbera =0; Private Double_numberb =0;  Public DoubleNumbera {Get{return_numbera;} Set{_numbera =value;} }         Public DoubleNumberb {Get{return_numberb;} Set{_numberb =value;} }         Public Virtual DoubleGetResult () {Doubleresult =0; returnresult; }    }

Op Subclass, I put it in the OperactionAll.cs.

    /// <summary>    ///Plus/// </summary>    classoperactionadd:operaction {/// <summary>        ///Operation Method/// </summary>        /// <returns></returns>         Public Override DoubleGetResult () {Doubleresult =0; Result= Numbera +Numberb; returnresult; }    }    /// <summary>    ///minus/// </summary>    classoperactionsub:operaction { Public Override DoubleGetResult () {Doubleresult =0; Result= Numbera-Numberb; returnresult; }    }    /// <summary>    ///Multiply/// </summary>    classoperactionmul:operaction { Public Override DoubleGetResult () {Doubleresult =0; Result= Numbera *Numberb; returnresult; }    }    /// <summary>    ///except/// </summary>    classoperactiondiv:operaction { Public Override DoubleGetResult () {Doubleresult =0; Result= Numbera/Numberb; returnresult; }    }

Factory class

    /// <summary>    ///Factory class/// </summary>     Public classOperactionfactory {//methods for creating instances         Public StaticOperaction Createoperaction (stringoperacte) {operaction oper=NULL; Switch(operacte) { Case "+": Oper=NewOperactionadd ();  Break;  Case "-": Oper=Newoperactionsub ();  Break;  Case "*": Oper=NewOperactionmul ();  Break;  Case "/": Oper=NewOperactiondiv ();  Break; }            returnOper; }    }

Client Calls

    /// <summary>    ///Client Calls/// </summary>    classProgram {Static voidMain (string[] args) {operaction oper=NULL; Oper= Operactionfactory.createoperaction ("-");//creating entities from a factoryOper. Numbera =Ten; Oper. Numberb=5; Doubleresult = oper. GetResult ();//Operation MethodConsole.WriteLine (Result);        Console.ReadLine (); }    }

Finished. Code: Click to download

01-Simple Factory

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.