Observer mode-Use of delegation

Source: Internet
Author: User

When I was learning C #, I left a question. Why does the program use delegation? In the end, what are the advantages that we sometimes choose to do more? Today I learned the Observer Pattern in the design pattern, and I learned some of the benefits of delegation!

For example of the Observer Pattern mentioned in the big talk design pattern, the class diagram of the observer pattern can be expressed:

So let's take a closer look at this example.

1. The observer who looks at the stock and the observer who watches the NBA must rely on the notification that the abstract notifiers give to them so that they can update their working status in time.

2. Abstract notifiers must rely on abstract observers to implement the notification function.

This is actually the core of the observer mode: defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can update themselves automatically.


In the beginning, we used the observer mode to reduce the coupling between two abstract subclasses (for example, foreground classes and stock-watching observer classes ), abstract An abstract class from the correlated subclasses of the two series to reduce coupling between classes. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + export/NvNbQo6yz6c/release + 3orP2tcTP + 8 + io6y527Ls1d + export/release + release/Wo7o8L3A + release = "brush: java; "> class Program {static void Main (string [] args) {// instantiate the Boss huhansan = new Boss (); // StockObserver tongshi1 = new StockObserver ("Tom ", huhansan); // NBA colleagues NBAObserver tongshi2 = new NBAObserver ("Xiao Li", huhansan ); // delegate the method to be executed by the specific observer to huhansan on the "Update" method of the "Boss. update + = new EventHandler (tongshi1.CloseStockMarket); huhansan. update + = new EventHandler (tongshi2.CloseNBADirectSeeding); // The boss returns to huhansan. sub JectState = "My boss is back! "; // Send a notification to huhansan. Y () ;}// notification class, which provides interface Subject {void Notify (); string SubjectState {get; set ;}} to specific observers ;}} // declare a delegate (event handler) delegate void EventHandler (); class Boss: Subject {// declare event Update, type: delegate EventHandler public event EventHandler Update; private string action; // The Boss state public void every Y () {Update ();} public string SubjectState {get {return action;} set {action = value ;}}} // Specific observer class, view the stock colleague class StockObserver {// No parameter constructor private string name; private Subject sub; public StockObserver (string name, Subject sub) {this. name = name; this. sub = sub;} public void CloseStockMarket () {Console. writeLine ("{0} {1} Close the stock market and continue to work! ", Sub. subjectState, name) ;}} class NBAObserver {private string name; private Subject sub; public NBAObserver (string name, Subject sub) {this. name = name; this. sub = sub;} public void CloseNBADirectSeeding () {Console. writeLine ("{0} {1} closes the NBA and continues working! ", Sub. SubjectState, name );}}

With the use of the preceding delegation, we can reduce the dependence of abstract notifiers on abstract observers in the original observer mode. This is the benefit of delegation.

It can reduce dependencies between classes and make the methods carried by the delegate object not belong to the same class (in this example, the delegate object is the observer class that looks at the stock and the observer class that looks at the NBA ).


Of course, there are certain requirements when using the delegate, that is, the delegate object methods must have the same prototype and form, that is, it has the same parameter list and return value type (in this example, both subclass objects have the same form ).


In this way, by combining with the observer mode, we can see the benefits of using delegation in C!


 






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.