Classic observer mode example)

Source: Internet
Author: User
Recently, when browsing in the garden, many of my friends are reading a classic C # interview question:
"The Cat is called, the mouse is running, and the host is awakened". C # is required.
Many friends in the garden have already implemented it. The specific method is implemented through the observer mode or event.
Here I will first extract the implementation of the observer mode, Code As follows: 1 Namespace Nickname
2 {
3 // The mouse ran and the host woke up.
4 // Use observer Mode
5 // Step 1 abstract observer
6 Public   Interface Observer
7 {
8 Void Response (); // Respond after receiving the event
9 }
10 // Step 2 Abstract The main object (CAT) that triggers the observer's response)
11 Public   Interface Subject
12 {
13 Void Aimat (Observer ob ); // The target object that will be used after the call
14 }
15 // Abstract mice and masters
16 Public   Class Mouse: Observer
17 {
18 Private   String Name;
19 Public Mouse ( String Name, subject sub) // Add observation on the subject object to the constructor (here we will observe the cat)
20 {
21 This . Name = Name;
22 Sub. aimat ( This );
23 }
24 Public   Void Response ()
25 {
26 Console. writeline ( " Cat cryed, "   + Name +   " Run! " );
27 }
28 }
29 Public   Class MASTER: Observer
30 {
31 Private   String Name;
32 Public Master ( String Name, subject sub) // Add observation on the subject object to the constructor (here we will observe the cat)
33 {
34 This . Name = Name;
35 Sub. aimat ( This );
36 }
37 Public   Void Response ()
38 {
39 Console. writeline ( " Cat cryed, "   + Name +   " Is weakup! " );
40 }
41 }
42 Public   Class Cat: Subject
43 {
44 Private List < Observer > Obs;
45 Public CAT ()
46 {
47 This . Obs =   New List < Observer > ();
48 }
49 Public   Void Aimat (Observer ob)
50 {
51 This . Obs. Add (OB );
52 }
53 Public   Void Cry ()
54 {
55 Foreach (Observer ob In   This . Obs)
56 {
57 Ob. Response ();
58 }
59 }
60 }
61 Class Program
62 {
63 Static   Void Main ( String [] ARGs)
64 {
65 Cat = New CAT ();
66 Mouse mouse1 =   New Mouse ( " Mouse1 " , CAT );
67 Mouse mouse2 =   New Mouse ( " Mouse2 " , CAT );
68 Master =   New Master ( " Xu " , CAT );
69 Cat. Cry ();
70 Console. Readline ();
71 }
72 }
73 }
Abstract ---- the essence of all design patterns.

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.