Design Pattern-Observer Pattern

Source: Internet
Author: User
Some time ago, I read the observer mode of the design model I talked about on the Internet. Oh, I remember a very good example. I am idle today. I just want to write it for a while.

In high school, the class leader is a female. N is beautiful and has a good figure. At which time, I don't know how many boys are in crush.
Haha, unfortunately, at that time, neither of the two men could find a good way, and even did not catch up with it. If we say that we are back to the past, haha, we will use the design model to explain this process of chasing beauty, at least, if I want to find the location where the beauty is today, I don't have to worry about it. You can say that I want to install a mobile service for her mobile phone, well, you can get her location at any time, but if you get her cell phone, you don't have to chase it. It's yours.

Haha,CodeLet's talk about it.

First, beauty

Public class beauty
{
Public event locationdelegate beauty location change;
Public beauty ()
{

}
Public void move (string mlocation)
{
This. Location = mlocation;
Onmove (location );

}
Protected void onmove (string mlocation)
{
If (this. Beauty location changed! = NULL)
{
This, new locationarg (mlocation ));
}
}
Private string location;
Public String location
{
Get
{
Return this. location;
}
}

Private string name;
Public string name
{
Get
{
Return name;
}
Set
{
Name = value;
}
}
Public override string tostring ()
{
Return this. Name. tostring ();
}
}

Public class locationarg: eventargs
{
Private string location;
Public locationarg (string mlocation)
{
This. Location = mlocation;
}
Public String location
{
Get
{
Return location;
}
}
}
Public Delegate void locationdelegate (Object sender, locationarg mlocationarg );

A boy's observation of changes in the beauty position:
 

 

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace observer
{
Public interface boys
{
String name
{
Get;
Set;
}
}
}

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace observer
{
Public class: Boys
{
Private beauty class leader;
Public a boy (beautiful squad leaders)
{
Beautiful squad leaders = Class leaders;
Beauty squad leader. Beauty location change + = new locationdelegate (beauty squad leader location change );
}

Change the position of the void beauty squad leader (Object sender, locationarg mlocationarg)
{
String location = mlocationarg. Location. tostring ();
}

private string mname;
Public string name
{< br> Get
{< br> return mname;
}< br> set
{< br> mname = value;
}< BR >}

Public override string tostring ()
{
Return this. Name. tostring ();
}


}
}

Public partial class form1: Form
{
Beauty 1 = new beauty ();
Public form1 ()
{
Initializecomponent ();
Boys and boys 1 = a new boy (beauty 1 );
}

Private void button#click (Object sender, eventargs E)
{
Beauty 1. Move ("Beauty in Jiefang Park ");
}

}
}

Haha, according to this design, boys will need to know where the beauty squad leader is at some time, so they don't have to call her on their own. Haha, as long as the position of the beauty squad leader changes, boys will be able to get the position of the beauty squad leader. Haha, how nice it is ..... wait for me... haha.

Read another articleArticle:
Http://www.cnblogs.com/ruochen/archive/2007/12/16/996706.html
A little confused,
Haha.
And the information of the observer registered at the observer? Or keep the information of the observer.

Well, let's take a look at the article above.

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.