Dota design pattern-Observer

Source: Internet
Author: User
Tags dota
The most obvious example in the life of the observer is newspaper subscription. At this time, the content of the newspaper is the subject, and every household who subscribes with money is the observer.
When you subscribe to a newspaper, The paper publisher will send the newspaper to your inbox on time. When you stop subscribing, your inbox will be empty. Of course, you can subscribe to or cancel the subscription at any time.
UML diagram:

The observer mode can process topic information in two ways: "Push mode" and "pull mode ". The most obvious observer mode in the framework is an event. You can use "+ =" to add an observer, or use "-=" to cancel the observer, the two parameters "sender" and "eventarg" of time must be regarded as the combination of Push mode and pull mode.
For example, btnxxx_click (Object sender, eventarg E) is an event.
1) in the push mode, the parameter passed is eventarg E, and the content in it is limited. It contains things that the topic wants the observer to know, which is helpful for controlling the information obtained by the observer. However, one problem is that if the theme adds new content, for example, the newly added "adult section", but eventarg does not include the "adult section ", now, you have to add the "adult section" in two places. One is the topic and the other is the eventarg class.
2) the PULL mode transmits the value of the topic to the observer rather than the observer. If the topic changes, you only need to change the topic category, in this example, the subject is our object sender.
3) The observer of the two modes has their own advantages and disadvantages. You can select the appropriate observer mode based on the actual needs of the project.
If you select the value (push mode) from the topic (button ):
Then: E. yyyy (unfortunately, the eventarg of the button is missing ).
If you directly operate the topic (button) (PULL mode), then:
Button button as sender;
If (button! = NULL)
{
// Obtain any value of the button, such as button. Text. Of course, this is based on the button passed as the sender.
}
In this example, as long as the topic implements the isubject interface, the observer implements the iobserver interface. For details, see the complete code:
Public   Interface Isubject
{
Void Addobserver (iobserver observer ); // Add an observer
Void Removeobserver (iobserver observer ); // Remove the observer
Void Notifyobserver (); // Notify all observers
Paramvalue getparamvalue ();
}
Public   Interface Iobserver
{
Void Update (isubject subject, paramvalue ); // After the observer receives the notification
}
Complete Code : Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. collections;

UsingDotacommon;

namespace dotapatternlibrary. observer
{< br> Public interface isubject
{< br> void addobserver (iobserver observer);
void removeobserver (iobserver observer);
void yyobserver ();
paramvalue getparamvalue ();
}

Public InterfaceIobserver
{
VoidUpdate (isubject subject, paramvalue );
}

Public ClassParamvalue
{
Private String_ Messagevalue;

Public string messagevalue
{< br> Get { return _messagevalue ;}< br> set {_ messagevalue = value ;}< BR >}

Public ClassGame: isubject
{
# RegionIsubject Member

Public VoidAddobserver (iobserver observer)
{
Observers. Add (observer );
}

Public VoidRemoveobserver (iobserver observer)
{
Observers. Remove (observer );
}

Public   Void Notifyobserver ()
{
Foreach (Iobserver observer In Observers. toarray ( Typeof (Iobserver )))
{
Observer. Update ( This , _ Paramvalue );
}
}

PublicParamvalue getparamvalue ()
{
Return_ Paramvalue;
}

# Endregion

Private Arraylist observers;
Private Paramvalue _ paramvalue;
Public Game ()
{
Observers =   New Arraylist ();
_ Paramvalue =   New Paramvalue ();
}

PublicParamvalue PV
{
Set{_ Paramvalue=Value ;}
}
}

Public ClassGameplayerone: iobserver
{
# RegionIobserver Member

Public   Void Update (isubject subject, paramvalue)
{
Landpyform. Form. outputresult ( " Gamerone (PUSH model ): "   + Paramvalue. messagevalue );
// Subject can be returned for the abstract class value without using a function.
Landpyform. Form. outputresult ( " Gamerone (PULL model ): "   + Subject. getparamvalue (). messagevalue );
}

# Endregion

PublicGameplayerone (isubject subject)
{
Subject. addobserver (This);
}

Public void outofsubject (isubject subject)
{< br> subject. removeobserver ( This );
}< BR >}

Public ClassGameplayertwo: iobserver
{
# RegionIobserver Member

Public   Void Update (isubject subject, paramvalue)
{
Landpyform. Form. outputresult ( " Gamertwo (PUSH model ): "   + Paramvalue. messagevalue );
// Subject can be returned for the abstract class value without using a function.
Landpyform. Form. outputresult ( " Gamertwo (PULL model ): "   + Subject. getparamvalue (). messagevalue );
}

# Endregion

PublicGameplayertwo (isubject subject)
{
Subject. addobserver (This);
}

Public void outofsubject (isubject subject)
{< br> subject. removeobserver ( This );
}< BR >}< br>

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.