Delegate / event and observer mode

 

Delegate is a class that defines the type of a method, like other methods such as the conventional type is passed as a parameter, but the difference from the conventional type that can be bound to a plurality of delegate the method of Example instance, call when, in sequence. Commissioned to bring the benefits of that program to enhance scalability to address the if / else ( or Switch) problems.

      Where it would delegate : when it comes to the same for a certain input, you need to perform different logical OR of different outputs according to the situation usually required if / else or switch implemented, consider the introduction of commission.

       The actual event is a delegate package, when the delegate is defined as a member of a class, if declared as public and fear of exposure to the client free to change, contrary to encapsulation, declared as Private , and contrary to the client using intention, and therefore using general method corresponding to the type attribute to solve, Event can be seen as a delegate property variables. Shaped like a public   Event   *** the delegate   ***;

      .net framework defined EventHandler is a general trust that receives object and EventArgs parameters. In order to maintain the package of events outside the class, the event only provide registration and deregistration, not for the call, the call can only be carried out in-house, while the interior is usually way to subscribe to an event, usually "On event name " as a name, for example page 's OnInit, OnLoad and so on.

       Asp.net in , the event is a typical application. Take System.Web.UI.Page class in terms of page life cycle consists of a series of events in ProcessRequest define this process. For example, the first implementation OnPreInit method, which calls PreInit event, if certain functions of the client register the PreInit event, it will be followed by the implementation of other events the same way.

      Observer is a design pattern. It is usually used to solve the problem - many dependent objects. It has 2 types, themes and observer. Theme subscribe or cancel observer, when the subject of a change, the observer will notice, conduct Update logic. This principle is the same incident.

      Observer pattern is divided into a push mode and a pull mode, when the difference between them is that relating to changes call push mode observer objects pass data parameters ( data parameters include information topics ) , and a pull mode, relating directly to the reference itself the object passed to the observer. The trouble is that the former need to define data parameters EventArgs , but on the subject of the object protection is better, at least not exposed to the reference; the latter need to implement data parameters, but the topic is likely to lead to tinker with. .net framework defined EventHandler (object sender, EventArgs e) use of these 2 modes.

Reproduced in: https: //www.cnblogs.com/JasonCrab/archive/2011/07/22/2113842.html

Guess you like

Origin blog.csdn.net/weixin_34378922/article/details/94278731