当前位置: 移动技术网 > IT编程>软件设计>设计模式 > 设计模式-观察者设计模式

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

2019年09月20日  | 移动技术网IT编程  | 我要评论
一个对象的动作触发多个对象的行为,通过观察者可以去掉对象的依赖,支持各种自定义和扩展。 观察者模式,还要从那只申请的猫开始说起。 猫叫一声之后触发: Baby Cry()、Brother Trun()、Dog Wang()、Father Roar()、Mothor Whisper()、Mouse R ...

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

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

猫叫一声之后触发:

  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);
    }
}

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

 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);
     }
 }

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();

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网