具体应用场景是,当subject的某个动作需要引发一系列不同对象的动作(比如你是一个班长要去通知班里的某些人),与其一个一个的手动调用触发的方法(私下里一个一个通知),不如维护一个列表(建一个群),这个列表存有你想要调用的对象方法(想要通知的人);之后每次做的触发的时候只要轮询这个列表就好了(群发),而不用关心这个列表里有谁,只用关心想让谁加入让谁退出
PHP版:
<span style="color: #008000;">//</span><span style="color: #008000;">这个是主题</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Paper{ </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$_observers</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> register(<span style="color: #800080;">$obj</span><span style="color: #000000;">){ </span><span style="color: #800080;">$this</span>->_observers[]=<span style="color: #800080;">$obj</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> start(){ </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">_observers)){ </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$this</span>->_observers <span style="color: #0000ff;">as</span> <span style="color: #800080;">$observer</span><span style="color: #000000;">){ </span><span style="color: #800080;">$observer</span>-><span style="color: #000000;">todo(); } } } } </span><span style="color: #008000;">//</span><span style="color: #008000;">这个是观察者</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> observer{ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> todo(){ </span><span style="color: #0000ff;">echo</span> "我在做..."<span style="color: #000000;">; } } </span><span style="color: #800080;">$paper</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Paper(); </span><span style="color: #800080;">$paper</span>->register(<span style="color: #0000ff;">new</span><span style="color: #000000;"> observer()); </span><span style="color: #800080;">$paper</span>->register(<span style="color: #0000ff;">new</span><span style="color: #000000;"> observer()); </span><span style="color: #800080;">$paper</span>->start();
javascript版:
<span style="color: #008000;">//</span><span style="color: #008000;">这是主题</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> Paper(){ </span><span style="color: #0000ff;">this</span>.observers=<span style="color: #000000;">[]; </span><span style="color: #0000ff;">this</span>.register=<span style="color: #0000ff;">function</span><span style="color: #000000;">(obj){ </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.observers.push(obj); } </span><span style="color: #0000ff;">this</span>.start=<span style="color: #0000ff;">function</span><span style="color: #000000;">(){ </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">var</span> i=0;ithis.observers.length;i++<span style="color: #000000;">){ </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.observers[i].todo(); } } } </span><span style="color: #008000;">//</span><span style="color: #008000;">这是观察者</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> Observer(){ </span><span style="color: #0000ff;">this</span>.todo=<span style="color: #0000ff;">function</span><span style="color: #000000;">(){ console.log(</span>"我在做..."<span style="color: #000000;">); } } </span><span style="color: #0000ff;">var</span> paper=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Paper(); paper.register(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Observer()); paper.register(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Observer()); paper.start();</span>
java版:
<span style="color: #0000ff;">package</span><span style="color: #000000;"> com.tsh.mthread; </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.ArrayList; </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.List; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Home { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) { Paper paper</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Paper(); paper.register(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Observer()); paper.register(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Observer()); paper.start(); } </span><span style="color: #008000;">//</span><span style="color: #008000;">这是主题</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Paper{ </span><span style="color: #0000ff;">private</span> List<observer> observers=<span style="color: #0000ff;">new</span> ArrayList<observer><span style="color: #000000;">(); </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> register(Observer observer){ observers.add(observer); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> start(){ </span><span style="color: #0000ff;">if</span>(observers.size()!=0<span style="color: #000000;">){ </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Observer observer:observers){ observer.todo(); } } } } </span><span style="color: #008000;">//</span><span style="color: #008000;">这是观察者</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Observer{ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> todo(){ System.out.println(</span>"我在做..."<span style="color: #000000;">); } } }</span></observer></observer>