Observer mode: A cat calls a mouse to wake up (C #),

Source: Internet
Author: User

Observer mode: A cat calls a mouse to wake up (C #),

Based on the idea of cainiao flying first, I decided to record the knowledge points that I encountered at ordinary times. One was to deepen my understanding, and the other was to facilitate future reference. When you see this article, please give me more advice on where the code is insufficient. Thank you. (The name is not standardized to deepen your understanding .)

Observer mode: When the status or behavior of the target object changes, the observer of the target object is observing to make a series of automatic responses.

1. interface implementation public interface Oberver {void response ();} public interface BObserver {void response (Oberver o);} public class mouse: Oberver {public mouse (BObserver B) {B. response (this);} public void sit response () {Console. writeLine ("the mouse ran... ") ;}} public class: Oberver {public (BObserver B) {B. response (this);} public void sit response () {Console. writeLine ("wake up... ") ;}} public class Cat: BObserver {List <Oberver> list; public CAT () {list = new List <Oberver> ();} public Void response (Oberver o) {list. add (o);} public void () {foreach (Oberver o in list) {o. sit out response () ;}} 2. delegate and event public delegate void processing (); public abstract class observed {public event processing event; public void Test () {if (Processing event! = Null) this. Handle the event () ;}} public class Cat: Observed {public void cat name () {Console. WriteLine ("cat name! "); This. test () ;}} public abstract class observer 2 {public observer 2 (observed B) {B. processing Event + = new Processing (observed to make a response);} public abstract void observation to make a response ();} public class mouse: observer 2 {public mouse (observed B ): base (B) {} public override void observed response () {Console. writeLine ("the mouse ran ...... ") ;}} public class person: observer 2 {public person (observed B): base (B) {} public override void observed response () {Console. writeLine ("wake up ...... ");}}

 

The first post is messy. -Cainiao xianfei

Related Article

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.