.net design patterns - Observer pattern

Behavioral Design Patterns: separation of concerns and behavior of objects

Observer: the Solution After an object is inside something happen, it will trigger a series of actions other objects, then we do not want to close dependence between objects  

There is a cat called the class that if the method of execution miao, will trigger a series of actions

 1 public class Cat
 2     {
 3         public void Miao()
 4         {
 5             Console.WriteLine("{0} Miao.....", this.GetType().Name);
 6 
 7             new Dog().Wang(3);
 8             //new Mouse().Run();
 9             new Chicken().Woo();
10             new Baby().Cry();
11             new Brother().Turn();
12             new Father().Roar();
13             new Mother().Whisper();
14             //new Mouse().Run();
15             new Neighbor().Awake();
16             new Stealer().Hide();
17         }
18 
19 }

But this resulted in the coupling, because if a little action or action plus a requirement to perform, and then, or need to change the name of the method call, which will need to modify at Cat's, too much trouble

So in this case should be a universal method to solve this problem - is transferred, the sequence (order) and in the end to be performed which are subject to the method of performing the upper end of the call to the specified person

 

1. observer pattern: object-oriented solution

The provisions of an interface, there is a method

1  public interface IObserver
2     {
3         void Action();
4     }

Each trigger actions are to achieve this interface

 1 public class Baby : IObserver
 2     {
 3         public void Action()
 4         {
 5             this.Cry();
 6         }
 7 
 8         public void Cry()
 9         {
10             Console.WriteLine("{0} Cry", this.GetType().Name);
11         }
12     }
 1  public class Brother : IObserver
 2     {
 3         public void Action()
 4         {
 5             this.Turn();
 6         }
 7         public void Turn()
 8         {
 9             Console.WriteLine("{0} Turn", this.GetType().Name);
10         }
11     }
 1  public class Chicken : IObserver
 2     {
 3         public void Action()
 4         {
 5             this.Woo();
 6         }
 7         public void Woo()
 8         {
 9             Console.WriteLine("{0} Woo", this.GetType().Name);
10         }
11     }

And so achieve action

Definition of cat class, add AddObserver interface that allows the upper to add actions

 1  public class Cat
 2     {
 3        
 4         private List<IObserver> _ObserverList = new List<IObserver>();
 5         public void AddObserver(IObserver observer)
 6         {
 7             this._ObserverList.Add(observer);
 8         }
 9         public void MiaoObserver()
10         {
11             Console.WriteLine("{0} MiaoObserver.....", this.GetType().Name);
12             if (this._ObserverList != null)
13             {
14                 foreach (var observer in _ObserverList)
15                 {
16                     observer.Action();
17                 }
18             }
19         }
20 }

transfer

 1   {
 2                     Console.WriteLine("************************");
 3                     Cat cat = new Cat();
 4                     cat.AddObserver(new Dog());
 5                     cat.AddObserver(new Mouse());
 6                     cat.AddObserver(new Chicken());
 7                     cat.AddObserver(new Baby());
 8                     cat.AddObserver(new Brother());
 9                     cat.AddObserver(new Father());
10                     cat.AddObserver(new Mother());
11                     cat.AddObserver(new Neighbor());
12                     cat.AddObserver(new Stealer());
13                     cat.MiaoObserver();
14                 }

 2. Observer pattern: Application event (multicast delegate) of solution

 cat definition

 1  public class Cat
 2     {
 3       
 4       
 5 
 6         public event Action MiaoHandler;
 7         public void MiaoEvent()
 8         {
 9             Console.WriteLine("{0} MiaoEvent.....", this.GetType().Name);
10             if (this.MiaoHandler != null)
11             {
12                 //foreach (Action action in this.MiaoHandler.GetInvocationList())
13                 //{
14                 //    action.Invoke();
15                 //}
16                 this.MiaoHandler.Invoke();
17             }
18         }
19 
20 
21 
22     }

transfer

 1   {
 2                     Console.WriteLine("************************");
 3                     Cat cat = new Cat();
 4                     //cat.MiaoHandler += new Action(new Dog().Wang);
 5 
 6                     cat.MiaoHandler += new Action(() => new Dog().Wang(3));
 7 
 8                     cat.MiaoHandler += new Mouse().Run;
 9                     cat.MiaoHandler += new Chicken().Woo;
10                     cat.MiaoHandler += new Baby().Cry;
11                     cat.MiaoHandler += new Brother().Turn;
12                     cat.MiaoHandler += new Father().Roar;
13                     cat.MiaoHandler += new Mother().Whisper;
14                     cat.MiaoHandler += new Neighbor().Awake;
15                     cat.MiaoHandler += new Stealer().Hide;
16                     cat.MiaoEvent();
17                 }

 

The observer pattern to help us solve a number of actions in the context of a method call to the number and order, and so the problem will change often occurs through the transfer of these two methods to the upper end, so that the upper end to deal with different situations

 

Guess you like

Origin www.cnblogs.com/Spinoza/p/11520526.html