Observer mode-I always know how to play games at work?

Source: Internet
Author: User

Original post and discussion: http://bbs.bccn.net/thread-213640-1-1.html

It's over 0. But you haven't slept yet. Maybe it's just two o'clock before you get used to it. I don't know what to do. It's boring. I don't want to read books because I am ill. I haven't completed the project yet, but I don't want to do it either. I haven't written anything for a long time. When I'm bored, let's record a story. It's a real story (A dummies believe it, a dummies do not believe it-Don't read itArticleIs your loss ).

Note: In the following examples, replace "C" with "M" and "A" as the boss ".

A classmate told me the other day: "I know how the boss knows about playing games when I go to work. It's hard for him to have a monitor in his office ."

"Well, it's hard to say, but it should not," m said :.

T: "How do I always know that I am playing games ?"

M: "I have to take a good look at this problem. Maybe he may have installed a camera somewhere in the office to monitor it. Haha !"

T: "Yes, I doubt it too, because I am out of the office, and other colleagues also know how to play games. This is amazing !"

M: "isn't he the only one who can monitor you? Also, he is still playing advanced, and he has a one-to-many relationship. And anyone playing games in your office can catch you exactly and on time. Your boss is powerful enough !"

M: "This reminds me of the knowledge of software design ."

The observer defines the one-to-many relationship between objects. When the State of an object changes, all objects dependent on it are notified and automatically updated.

T: "Are you crazy about software? Can this be related to software design? Don't talk about it. Let's talk about it ."

M: "What I am talking about is the main thing. Ah, you still have a bad temper and don't change your mind. If you haven't finished talking about others, you can interrupt others and insert a few words on your own. You should also talk about principles, let others talk about it. You are receiving it. You heard it first. You're talking about it ."

M: "You should take a closer look at the software design knowledge point I mentioned above. Your boss can monitor multiple of you. When you play games, he will be able to catch you on time.ProgramIs the playgame () method called when you play a game? When the playgame () method is monitored, when playgame () is called, it automatically notifies the Monitor (your boss). Obviously, he knew that you were taking his money for dinner and didn't do anything for him. Instead, he played games in the office and caught you. But you have to think too much about it. There is also a reason for the boss to do this. After all, he wants to get a return if he invests in the capital, right ?"

T: "Stop, stop. You don't have to worry about me. As you can see, when it comes to software design, you may wish to finish singing what you have mastered !"

M: "Haha, each other !"

---- Object-oriented design mode [observer mode]

T: "To tell the truth, I don't do the program for many days, but I still remember that day! When I hear you say this, I am a little interested in this software design solution. Can you tell me more ?"

M: "Well, it's rare for you to have this interest. I will use an example to give you a brief introduction !"

T: "I have said that many friends have multiple roads. Well, many students have chatted with each other and talked with others. Well, let's just say a little bit. Don't let that guy hear it ."

M: "whether you are using. Net or Java for development, the event model of these two platforms is a typical observer model. I will give a brief example in the C # language of the. NET platform ."

M: "First, we need to define a delegate to bind two classes of interaction through the delegate. When an employee calls the palygame () method to play the game, it triggers a delegate-type playgame event, this event will be delegated to the call boss (notify) method of the boss (Admin) for handling."
/// <Summary>
/// Define the delegate
/// </Summary>
Public Delegate void delegatemonitoremployee (Object sender, customeevetnargs E );

M: "If you are a person and use object-oriented thinking, are you an object? Abstract Human objects. In the company, if you are an employee, then we can abstract the employee Employee object in computer language. You want to play games, in addition, you will be caught by the boss when playing a game. You have to do a playgame method. Here we obviously need to define a delegate-type event, associate the game method games () with events."

// Employee type
Public class employee
{
Private string _ name;
Public string name
{
Get {return _ name ;}
Set {_ name = value ;}
}

Private int _ age;
Public int age
{
Get {return _ age ;}
Set {_ age = value ;}
}

/// <Summary>
/// Delegate-type event
/// </Summary>
Public event delegatemonitoremployee playgame;

Public void Gages ()
{
If (playgame! = NULL)
{
Customeevetnargs E = new customeevetnargs ();
E. Name = This. _ name;
E. Age = This. _ age;
Playgame (this, e );
}
}
}
T: "What is your customeevetnargs? I didn't seem to have seen this stuff before when I made a program. Is it defined by myself ?"

M: "Good? Will the problem be found? Yes, customeevetnargs is a class defined by myself. It inherits from the eventargs class of the system and is used to transmit messages. You will know it later, you play the game by passing your name and other information to the boss. Since you are so anxious to know what he is doing, let's look at the following definition ."
/// <Summary>
/// Eventargs is used to transmit event data.

/// Defines two Members, which can pass two data (name, age)
/// </Summary>
Public class customeevetnargs: eventargs
{
String name = string. empty;
Public string name
{
Get {return name ;}
Set {name = value ;}
}

Int age = 0;
Public int age
{
Get {return age ;}
Set {age = value ;}
}

Public customeevetnargs ()
{}
}

M: "Let's take a look at how the boss (Admin) Class handles it. When an employee (employee) plays a game (called playgame), how can he always be notified ."
// Old class
Public class Admin
{
/// <Summary>
/// Call the boss --

/// Pass the employee information of the game through the customeevetnargs event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Public void Policy (Object sender, customeevetnargs E)
{
Stringbuilder resutl = new stringbuilder ();
Resutl. append ("Employee:" + E. Name + "");
Resutl. append ("Age:" + E. Age. tostring () + "years ");
Resutl. append ("playing a game! ");
Resutl. append ("time:" + datetime. Now. tostring ());
Console. writeline (resutl );
}
}

M: "here, we have fully established the entire monitoring process. Now is the time to experiment. Old employee (employee), are you going to play games and try to see if the boss (Admin) can build your system through the observation (Monitoring) that we have spent so much effort on ?"

T: "You are better at speaking than singing, just a few dozen words.CodeAre you so powerful? Don't believe it, okay. I'll try it ."

-- Michael Zhang (I forgot to say that my classmate is Michael Zhang, who is 22 years old .) Turn on the computer and start the game.
Class Program
{
Static void main (string [] ARGs)
{
Employee Employee = new employee ();
Employee. Name = "James ";
Employee. Age = 22;
Admin admin = new admin ();

// Bind the interaction between the two classes through delegatemonitoremployee. After the employee. Games method is called,
// Trigger the playgame event, which will be delegated to the notify method of Admin for processing.
Employee. playgame + = new delegatemonitoremployee (Admin. Policy );
Employee. Gages ();
}
}

-- At this moment, the alarm in the boss's office sounded and sent a signal;
 

-- John just started the game and suddenly opened the office door. A "Person (Admin)" is entered )".

A: "James, are you brave enough to play the game yesterday? You were fined 100 oceans for playing the game yesterday. Are you still playing the game today? We will talk to you later in my office !"

-- Haha, I guess this time I am going to be whitewashed .... Waiting to be copied .......

The article is everywhere. I hope you can provide more support.

 

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.