设计模式-观察者设计模式

一个对象的动作触发多个对象的行为,通过观察者可以去掉对象的依赖,支持各种自定义和扩展。

观察者模式,还要从那只申请的猫开始说起。

猫叫一声之后触发:

  Baby Cry()、Brother Trun()、Dog Wang()、Father Roar()、Mothor Whisper()、Mouse Run()、Neighbour Awake()、Stealer Hide().......

下面代码是这些触发类:

public class Baby
{
    public void Cry()
    {
        Console.WriteLine("{0} Cry", this.GetType().Name);
    }
}

public class Brother
{
    public void Turn()
    {
        Console.WriteLine("{0} Turn", this.GetType().Name);
    }
}

 public class Chicken
 {
     public void Woo()
     {
         Console.WriteLine("{0} Woo", this.GetType().Name);
     }
 }

 public class Dog
 {
     public void Wang()
     {
         Console.WriteLine("{0} Wang", this.GetType().Name);
     }
 }

 public class Father
 {
     public void Roar()
     {
         Console.WriteLine("{0} Roar", this.GetType().Name);
     }
 }

public class Mother
{
    public void Whisper()
    {
        Console.WriteLine("{0} Whisper", this.GetType().Name);
    }
}

public class Mouse
{
    public void Run()
    {
        Console.WriteLine("{0} Run", this.GetType().Name);
    }
}

 public class Neighbor
 {
     public void Awake()
     {
         Console.WriteLine("{0} Awake", this.GetType().Name);
     }
 }

public class Stealer
{
    public void Hide()
    {
        Console.WriteLine("{0} Hide", this.GetType().Name);
    }
}

View Code

然后,猫叫一声,触发这些动作:

public class Cat
 {
     public void Miao()
     {
         Console.WriteLine("{0} Miao.....", this.GetType().Name);

         new Mouse().Run();//依赖
         new Chicken().Woo();
         new Baby().Cry();
         new Brother().Turn();
         new Dog().Wang();
         new Father().Roar();
         new Mother().Whisper();
         //new Mouse().Run();
         new Neighbor().Awake();
         //new Stealer().Hide();
     }
}

这样写,功能是实现了,但是依赖太多,任何一个对象改动,都会导致Cat类变化,违背了单一职责,不仅自己Miao,还要触发各种动作,不稳定,加一个/减一个/调整顺序,Cat都得改。

Cat职责:

  1、Miao()

  2、触发一些列动作,这其实就是需求

  3、实现上,其实多了一个,指定动作

Cat不稳定的原因,在Miao()方法里面的一些对象。

这个时候就要甩锅大法了,把锅丢出去与,只管自己,把不稳定的地方移出去,自己只写稳定的,这样能保证自身的稳定。

定义一个接口,把这些动作抽象出一个Action(),只是为了把多个对象产生关系,方便保存和调用,方法本身其实没用的:

public interface IObserver
{
    void Action();
}

让上面那些Miao一声后需要触发的动作类,实现这个接口:

public class Baby : IObserver
{
    public void Action()
    {
        this.Cry();
    }

    public void Cry()
    {
        Console.WriteLine("{0} Cry", this.GetType().Name);
    }
}

public class Brother : IObserver
{
    public void Action()
    {
        this.Turn();
    }
    public void Turn()
    {
        Console.WriteLine("{0} Turn", this.GetType().Name);
    }
}

 public class Chicken : IObserver
 {
     public void Action()
     {
         this.Woo();
     }
     public void Woo()
     {
         Console.WriteLine("{0} Woo", this.GetType().Name);
     }
 }

public class Dog : IObserver
{
    public void Action()
    {
        this.Wang();
    }
    public void Wang()
    {
        Console.WriteLine("{0} Wang", this.GetType().Name);
    }
}

public class Father : IObserver
{
    public void Action()
    {
        this.Roar();
    }
    public void Roar()
    {
        Console.WriteLine("{0} Roar", this.GetType().Name);
    }
}

public class Mother : IObserver
{
    public void Action()
    {
        this.Whisper();
    }
    public void Whisper()
    {
        Console.WriteLine("{0} Whisper", this.GetType().Name);
    }
}

 public class Mouse : IObserver
 {
     public void Action()
     {
         this.Run();
     }
     public void Run()
     {
         Console.WriteLine("{0} Run", this.GetType().Name);
     }
 }

 public class Neighbor : IObserver
 {
     public void Action()
     {
         this.Awake();
     }
     public void Awake()
     {
         Console.WriteLine("{0} Awake", this.GetType().Name);
     }
 }

 public class Stealer : IObserver
 {
     public void Action()
     {
         this.Hide();
     }
     public void Hide()
     {
         Console.WriteLine("{0} Hide", this.GetType().Name);
     }
 }

View Code

Cat这个类就可以修改成如下,使用集合或者事件都是可以的:

public class Cat
{
    //Cat不稳定--这一堆对象--甩锅--自己不写让别人传递
    private List<IObserver> _ObserverList = new List<IObserver>();
    public void AddObserver(IObserver observer)
    {
        this._ObserverList.Add(observer);     MiaoHandler += observer.Action;
    }
    public void MiaoObserver()
    {
        Console.WriteLine("{0} MiaoObserver.....", this.GetType().Name);
        if (this._ObserverList != null && this._ObserverList.Count > 0)
        {
            foreach (var item in this._ObserverList)
            {
                item.Action();
            }
        }
    }

    private event Action MiaoHandler;
    public void MiaoEvent()
    {
        Console.WriteLine("{0} MiaoEvent.....", this.GetType().Name);
        if (this.MiaoHandler != null)
        {
            foreach (Action item in this.MiaoHandler.GetInvocationList())
            {
                item.Invoke();
            }
        }
    }
}

这样在调用的时候,增加一个,减少一个,更改顺序,就不要再去修改Cat类了。

Cat cat = new Cat();
 cat.AddObserver(new Chicken());
 cat.AddObserver(new Baby());
 cat.AddObserver(new Brother());
 cat.AddObserver(new Dog());
 cat.AddObserver(new Father());
 cat.AddObserver(new Mother());
 cat.AddObserver(new Mouse());
 cat.AddObserver(new Neighbor());
 cat.AddObserver(new Stealer());
 cat.MiaoObserver();
(0)

相关推荐

  • VB.net中动态构建Linq的Where子句

    Imports System.Linq.Expressions Imports System.Linq Imports System.Collections.Generic Class A     P ...

  • 设计模式(20) 观察者模式

    观察者模式是一种平时接触较多的模式.它主要用于一对多的通知发布机制,当一个对象发生改变时自动通知其他对象,其他对象便做出相应的反应,同时保证了被观察对象与观察对象之间没有直接的依赖. GOF对观察者模 ...

  • 面向对象23种设计模式系列(一)- 创建型设计模式

    本章是面向对象23种设计模式系列开篇,首先我们来看下什么是设计模式? 面向对象23种设计模式: 1.面向对象语言开发过程中,遇到的种种场景和问题,提出了解决方案和思路,沉淀下来就变成了设计模式. 2. ...

  • 23种设计模式入门 - 设计模式概述及七大原则

    设计模式的目的 使程序拥有更好的的 代码复用性(一次编译,处处运行[手动狗头]) 可读性(不可替代性堪忧呀) 可扩展性(新增功能时方便) 可靠性(新增功能后对旧功能没有影响) 高内聚,低耦合 设计模式 ...

  • 图解Java设计模式之设计模式七大原则

    图解Java设计模式之设计模式七大原则 2.1 设计模式的目的 2.2 设计模式七大原则 2.3 单一职责原则 2.3.1 基本介绍 2.3.2 应用实例 2.4 接口隔离原则(Interface S ...

  • 设计模式:超越软件与设计的模式语言

    最近看了一本书<架构启示录>,书中研究传统的建筑工作与数字产品的架构工作之间有着怎样的联系,重点探讨了Christopher Alexander.Richard Saul Wurman.C ...

  • 字节大牛总结的设计模式火了

    据说有小伙伴靠这份笔记顺利进入 BAT 哦,所以一定要好好学习这份资料! 资料介绍 这份资料非常全面且详细,覆盖了 设计模式 基础学习的方方面面,非常适合初学者入门! 资料也按目录进行编排,每一章下面 ...

  • 嵌入式C语言开发者必知:“设计模式”

    介绍如何使用设计模式为嵌入式系统创建高效且优化的C语言设计.针对嵌入式系统中从内存访问到事件调度,从状态机设计到安全性.可靠性保证,对系统设计以及性能表现的方方面面进行了详细阐述,也提出了很好的设计规 ...

  • 设计模式(一)——Java单例模式(代码+源码分析)

    设计模式(一)——Java单例模式(代码+源码分析)

  • 设计模式(七)——适配器模式(SpringMVC框架分析)

    适配器模式1 现实生活中的适配器例子泰国插座用的是两孔的(欧标),可以买个多功能转换插头 (适配器) ,这样就可以使用了. 2 基本介绍1) 适配器模式(Adapter Pattern)将某个类的接口 ...

  • 洞察设计模式的底层逻辑

    设计模式是开发同学经常聊到的话题,也经常被用到实际的开发项目中,熟练的人可以做到信手拈来,不熟悉的人陷入苦思冥想中.笔者认为,不仅仅要掌握设计模式的用法,更要洞察设计模式的底层逻辑,只有那样,才能做到 ...

  • 设计模式(十)——组合模式(HashMap源码解析)

    设计模式(十)——组合模式(HashMap源码解析)