Observer pattern: Cat called Rat Runner wakes up (C #)

Source: Internet
Author: User

In the first flight of the idea of beginners, decided to the usual knowledge points recorded down, one is to deepen understanding, and secondly is convenient to consult later. When you see my article, I feel that the code is where the lack of places, please give more advice, thank you. (the naming is not standardized in order to deepen their understanding, a lot of forgive.) )

Observer pattern: When the target object state or behavior changes, observers who are observing the target object make a series of automatic responses.

1. Interface Implementation Public Interfaceoberver{voidsit out reaction ();} Public Interfacebobserver{voidreaction (Oberver o);} Public classMouse: oberver{ PublicMouse (Bobserver b) {B. reaction ( This);} Public voidsit-out reaction () {Console.WriteLine ("the mouse ran away ...");}} Public classPerson: oberver{ Publicperson (Bobserver b) {B. reaction ( This);} Public voidsit-out reaction () {Console.WriteLine ("people wake up ...");}} Public classCat: Bobserver{list<Oberver>list; PublicCat () {list=NewList<oberver>();} Public voidreaction (Oberver o) {list. ADD (o);} Public voidCat's name () {foreach(Oberver oinchlist) {O. Sit-out reaction ();}}2. Delegates and Events Public Delegate voidprocessing (); Public Abstract classbe observed { Public Eventdeal with event handling; Public voidTest () {if(Handle Event! =NULL) This. Handling events ();} } Public classCat: Being observed { Public voidCat's name () {Console.WriteLine ("the cat's barking! "); This. Test ();}} Public Abstract classViewer 2{ PublicObserver 2 (observed B) {B. Handling events+=NewTo deal with (an observation reacts);} Public Abstract voidobserve to react ();} Public classMouse: Viewer 2{ PublicMouse (observed B):Base(b) {} Public Override voidobservation to react () {Console.WriteLine ("the mouse ran away ...");}} Public classPerson: Viewer 2{ PublicMan (observed B):Base(b) {} Public Override voidobservation to react () {Console.WriteLine ("people wake up ...");}}

The first time post feel good chaos, a lot of included. -Rookie fly first.

Observer pattern: Cat called Rat Runner wakes up (C #)

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.