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