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: I am using a calculator applet to achieve. 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>///+ </summary> class Operactionadd:operaction {//&LT;SUMMARY&G        T        Calculation method///</summary>///<returns></returns> public override double GetResult ()            {double result = 0;            result = Numbera + Numberb;        return result; }}///<summary>////</summary> class Operactionsub:operaction {public over            Ride Double GetResult () {double result = 0;            result = Numbera-numberb;        return result; }}///<summary>///</summary> class Operactionmul:operaction {public over            Ride Double GetResult () {double result = 0;            result = Numbera * NUMBERB;        return result; }}///<summary>//except for//</summary> class Operactiondiv:operaction {public oveRride Double GetResult () {double result = 0;            result = Numbera/numberb;        return result; }    }

Factory class

    <summary>/    //Factory class/    //</summary> public class Operactionfactory    {        //method to create an instance Public        static Operaction createoperaction (string operacte)        {            operaction oper = null;            Switch (OPERACTE)            {case                "+":                    oper = new Operactionadd ();                    break;                Case "-":                    oper = new Operactionsub ();                    break;                Case "*":                    oper = new Operactionmul ();                    break;                Case "/":                    oper = new Operactiondiv ();                    break;            }            return oper;        }    }

Client Calls

    <summary>    ///Client call//</summary> class program {        static void Main (string[] args )        {            operaction oper = null;            Oper = operactionfactory.createoperaction ("-"); Create entity Oper from the factory            . Numbera = ten;            Oper. Numberb = 3;            Double result = oper. GetResult ();//Operation Method            Console.WriteLine (result);            Console.ReadLine ();        }    }

Finished.

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.