Yllen | Ring3.xyz

Yllen | Ring3.xyz

要去看埃菲尔铁塔的顶

欢迎关注本人微博:t.cn/RGSLVUk
查看介绍

观察者模式

概述:

    定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。


参与者

  1.Subject(目标)    

      目标知道它的观察者。可以有任意多个观察者观察同一个目标。      提供注册和删除观察者对象的接口。  

  2.Observer(观察者)   

       为那些在目标发生改变时需获得通知的对象定义一个更新接口。  

  3.ConcreteSubject(具体目标)  

        将有关状态存入各ConcreteObserver对象。      当它的状态发生改变时,向它的各个观察者发出通知。 

   4.ConcreteObserver(具体观察者)   

          维护一个指向ConcreteSubject对象的引用。      存储有关状态,这些状态应与目标的状态保持一致。    实现Observer的更新接口以使自身状态与目标的状态保持一致


注释:


    市民里面会有一个注册 好几个警察对象
    向对象发送消息时,
    市民内部会向所有注册监听的警察对象调用相同的
    action动作
    也即 市民是个广播者
    警察是接受者
    而接受时默认动作就是action
    具体消息内容会在市民对象内部
    接受者可以使用公共接口所传入的广播者对象 get相关消息




import java.util.*;
//subject
abstract class Citizen
{
    List pols;

    String help = "normal";

    public void setHelp(String help)
    {
        this.help = help;
    }

    public String getHelp()
    {
        return this.help;
    }

    abstract void sendMessage(String help);

    public Citizen()
    {
        this.pols = new ArrayList();
    }
    public void Register(Policemen pol)
    {
            this.pols.add(pol);
    }
    public void unRegister(Policemen pol)
    {
        this.pols.remove(pol);
    }
}

// Observe
interface Policemen
{
    void action(Citizen ci);
}

class HuangPuCitizen extends Citizen
{

    public void sendMessage(String help)
    {
        setHelp(help);
        for(int i=0;i<pols.size();i++)
        {
            Policemen pol = (Policemen)pols.get(i);
            pol.action(this);
        }
    }
}

class ShuiMuCitizen extends Citizen
{
    public void sendMessage(String help)
    {
        setHelp(help);
        for(int i=0;i<pols.size();i++)
        {
            Policemen pol = (Policemen)pols.get(i);
            pol.action(this);
        }
    }
}

class ShuiMuPolicemen implements Policemen
{
    public void action(Citizen ci)
    {
        String help = ci.getHelp();
        if(help.equals("normal"))
        {
            System.out.println(" everything is ok");
        }
        if(help.equals("unnormal"))
        {
            System.out.println(" Exception~  ShuiMuPoliceme Let's go!");
        }
    }
}

class HuangPuPolicemen implements Policemen
{
    public void action(Citizen ci)
    {
        String help = ci.getHelp();
        if(help.equals("normal"))
        {
            System.out.println("everything is ok");
        }
        if(help.equals("unnormal"))
        {
            System.out.println(" Exception~ HuangPuPolicemen Let's go! ");
        }
    }
}

//test

public class observe
{
    public static void main(String[] args)
    {
        Policemen hppol = new HuangPuPolicemen();
        Policemen smpol = new ShuiMuPolicemen();

        Citizen citizen = new HuangPuCitizen();
        citizen.Register(hppol);

        citizen.sendMessage("unnormal");
        citizen.sendMessage("normal");
        
        System.out.println("============");

        citizen = new HuangPuCitizen();
        citizen.Register(smpol);
        citizen.sendMessage("unnormal");
        citizen.sendMessage("normal");

    }
}


运行结果:


c:\Java\source>java observe


Exception~ HuangPuPolicemen Let's go!
everything is ok
============
Exception~  ShuiMuPoliceme Let's go!
everything is ok

评论
© Yllen | Ring3.xyz | Powered by LOFTER