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