Review the delegation and event that you can understand under the object-oriented core

Time:2019-11-5

 

The core of case interpretation is object-oriented. All cases are based on C ා, involving the most frequently concerned issues in our practice:

1. Encapsulation, inheritance and polymorphism;

2. Abstract class and interface;

3. Delegation and event.

III. entrustment and events

Generally speaking, the purpose of using delegation is to “realize the effect of passing methods as parameters”, which is illustrated by examples.

Let’s go ahead and change based on the last example.

Scene setting: return different area algorithms according to different graphics.

//Parameter is the shape of the figure, and returns the area calculation formula of the figure

        public string GetAreaAlg(string shapeName)

        {

            If (shapename = = "Rectangle")

            {

                Return "length × width";

            }

            Else if (shapename = = "circle")

            {

                return "pi * r * r";

            }

            else

            {

                Return "the graph area algorithm is not defined";

            }

        }

 

As more and more graphs are drawn, the branches in the method body expand.

At this time, we naturally think that if we can directly pass methods as parameters, then we can eliminate these branches.

Similar to the following:

Public string getareaalg

        {

            The method of calculating the area formula;

        }

 

 

Well, let’s follow this idea to transform:

#Demonstration of region delegate

        //Declare a delegate

        public delegate string GetAreaAlgDelegate();

 

        //Area algorithm of rectangle

        public string GetRectangleAreaAlgorithm()

        {

            Return "area algorithm of rectangle: length × width";

        }

        //Area algorithm of circle

        public string GetRCircleAreaAlgorithm()

        {

            Return "circle area formula: pi * r * r";

        }

 

        public string GetAreaAlg(GetAreaAlgDelegate GetAlgorithm)

        {

            return GetAlgorithm();

        }

 

        #endregion

 

 

At this time, you can call directly in home / index:

            string strAlgorith = GetAreaAlg(GetRectangleAreaAlgorithm);

 

 

Add a breakpoint to debug, you can see that the area algorithm of the rectangle is returned: length × width

Do you understand? Let’s summarize.

Delegate is the encapsulation of function. It can be used to assign a name to the feature of method. We can understand it by analogy with ordinary parameters.

Delegation is a class, which defines the type of method, so that the method can be passed as the parameter of another method. This method can dynamically assign the method to the parameter, which can avoid using a lot of branch statements in the program, and make the program have better scalability.

 

Let’s talk about the incident next.

Event is a special form of delegation. When an event occurs, the event object notifies the process.

Let’s change our example, add a sentence, and then declare the following events:

//Declare a delegate

        public delegate string GetAreaAlgDelegate();

 

        //Declare an event

        public event GetAreaAlgDelegate GetArea;

 

 

Also in home / index, register the event first when calling:

            GetArea += new GetAreaAlgDelegate(GetRCircleAreaAlgorithm);

            string res= GetArea();

 

Add a breakpoint to debug. You can see that the circular area formula is returned: pi * r * r

We register the getrcirclearareaalgorithm method into the event getarea by instantiating the delegate. Do you understand?

 

We click the mouse button, trigger an operation, and execute a method is a typical application.

 

Delegates and events are actually applications of the observer pattern.

I will not expand the specific application scenarios. You can think about them.

Just use two pictures to illustrate an example to inspire you to think.

Observer pattern structure:

 

 

Example of observer pattern:

 

 

I wish you progress in your study.

 

 

 

Recommended Today

Detailed explanation of sshd service and service management command under Linux

sshd SSH is the abbreviation of secure shell, which is the security protocol of application layer. SSH is a reliable protocol which provides security for remote login session and other network services. SSH protocol can effectively prevent information leakage in the process of remote management. openssh-server Function: enable remote hosts to access the sshd service […]