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 #)