Object-oriented design patterns and delegates

Source: Internet
Author: User

Factory mode

     Public classJisuan {Private intA;  Public intA {Get{returnA;} Set{a =value;} }        Private intb;  Public intB {Get{returnb;} Set{b =value;} }         Public Virtual intYunsuan () {return 0; }    }    //Addition Class     Public classJia:jisuan { Public Override intYunsuan () {return Base. A +Base.        B }    }    //Subtraction Class     Public classJian:jisuan { Public Override intYunsuan () {return Base. ABase.        B }    }    //Factory class     Public classGongchang { Public StaticJisuan Duixiang (strings) {Switch(s) { Case "+":                    return NewJia ();  Break;  Case "-":                    return NewJian ();  Break;  Case "*":                    return NewCheng ();  Break; default:                    return NewJia ();  Break; }        }    }

Single-Case mode

    classDbda {//Build Variables         Public stringhost;  Public stringdatabase; //a static member that is used to store the object of the class         Public StaticDBDA db =NULL; //construct method to let the class not be instantiated        PrivateDbda () {}//provide a way to create objects, control only one object         Public StaticDbda Duixiang ()//because I can't make objects, I use static .        {            if(db = =NULL) {db=NewDbda (); }            returnDB; }    }    classProgram {Static voidMain (string[] args) {DBDA db=Dbda.            Duixiang (); Db.host="localhost"; Dbda DB1=Dbda.                        Duixiang ();                        Console.WriteLine (Db1.host);        Console.ReadLine (); }    

Commissioned
Also known as a proxy, an event is also a delegate
Defined at the outermost of the class

1. Define the delegate
Keyword: delegate
function Signature : signature and function remain consistent
When defining a delegate, you define it according to the function.
public delegate int first (int a,int b);
The return type of the method pointed to requires the parameters to be consistent!

2. Defining the delegate variable, pointing to the method

A delegate cannot be instantiated because it is not a class

First F = new Jiafa (). Jiafa; New delegate variable, point to method, note!! method does not need parentheses!!

The second time you can use + =

public int Jiafa (int a,int b)
{
return a+b;
}

Call:
f (5,3);

Can be understood as a pointer to a function, which function the delegate points to, which function is represented by the delegate
Allows functions to be passed as arguments

    //Defining Delegates     Public Delegate voidSuibian (strings); classProgram {Static voidMain (string[] args) {            //Delegate//parameterization of the methodSuibian s =China ; S+ = America;//+ = is the binding method = minus a method//calling a speech methodSpeak (s);            Console.WriteLine ();        Console.ReadLine (); }        //methods of speech function        Static voidSpeak (Suibian yu) {yu ("Zhang San"); }        //Voice Pack         Public Static voidAmerica (strings) {Console.WriteLine (S+"Hello"); }        Static voidChina (strings) {Console.WriteLine (S+"Hello"); }        Static voidHanyu (strings) {Console.WriteLine (S+"Bjdkaj"); }      }

※ Event

Events are a special kind of delegate

Object-oriented design patterns and delegates

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.