学习、探究Java设计模式——观察者模式

来源:互联网 发布:cad网络培训班 编辑:程序博客网 时间:2024/05/21 19:50
http://blog.csdn.net/a553181867/article/details/52454178
http://blog.csdn.net/a553181867/article/details/52454178
http://blog.csdn.net/a553181867/article/details/52454178

 

学习、探究Java设计模式——观察者模式

标签: javaandroidjava设计模式面向对象编程设计模式
 425人阅读 评论(4) 收藏 举报
 分类:

目录(?)[+]

前言

观察者模式是面向对象编程中较为常用的一种设计模式,观察者模式又称为订阅-发布模式,特别是适用于GUI图形界面中,比如Android的View中就大量使用了此模式。那么观察者模式到底是什么以及我们应该怎么使用它呢?相信通过本文的学习,你们会有一个更为清晰的答案。

定义

观察者模式:定义了对象之间的一对多依赖,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。

由以上的定义,我们可以知道,观察者模式有两个重要要素:观察者、被观察者。用生活中一个常见的例子来描述:我们通常会订阅天气预报信息,假如天气出现了变化,气象App就会通知我们天气变了要做出相应的准备,那么在这个场景中,我们就充当了“观察者”角色,而气象App则充当了“被观察者”角色,当天气发生了变化,“被观察者”就会通知我们,让我们做出相应改变。

那么,我们已经知道了观察者和被观察者的概念,但我们还不知道它们两个是怎样联系起来的,接下来我们正要解决这个问题,为了更好地理解,我们先来看看观察者模式的UML类图。

UML类图

UML 
我们来分析下上图各个类或者接口的含义: 
Observerable:被观察者接口,规定了几个方法,分别是registerObserver():表示将观察者注册到被观察者中,即“订阅”;removeObserver():表示将观察者从被观察者中移除,即“取消订阅”;notifyObservers():当被观察者状态改变的时候,这个方法被调用,通知所有已经注册的观察者。

ConcreteObserverable:被观察者,实现了Observerable接口,对以上的三个方法进行了具体实现,同时有一个List集合,用以保存注册的观察者,等需要通知观察者时,遍历该集合即可。注意,该集合的泛型参数应该是Observer,接口类型,而不应该是具体的Observer实现类,这样做的原因是一个被观察者可能会有多个不同实现类的观察者(但都实现了Observer接口),如果限定了某一个具体的类型,那么会导致以后要加入新类型的时候而不得不修改当前类,耦合度过高,这是一个非常不好的行为。(设计原则:面向接口编程而不是面向实现编程

Observer:观察者接口,规定了update()方法,当被观察者调用notifyObservers()方法时,观察者的update()方法会被回调。

ConcreteObserver:观察者,实现了update()方法。

那么了解了以上各部分的含义后,应该不难得出观察者模式的实现过程了,那么我们接下来看看实现观察者模式的一般过程。

实现步骤

我们需要准备Observerable接口,声明注册、移除、通知更新这三个方法,接着需要Observer接口,声明update()方法,然后我们要实现以上两个接口,分别是ConcreteObserverable和ConcreteObserver这两个实现类。其中,ConcreteObserverable应该有一个成员变量用户保存注册的观察者,当需要通知更新的时候,调用ConcreteObserverable#notifyObservers()方法,该方法内部遍历所有的观察者,并调用他们的update()方法,这样便实现了订阅——发布流程。

A Sample

接下来,我们实现一个简单的例子,让大家更熟悉观察者模式。 
我们来模拟一个场景:A、B、C三人在书店订阅了同一本杂志,当该杂志上架的时候,会通知他们杂志上架的信息(包括杂志的期数、最新价格),让他们过来购买。

Step 1.建立接口

新建Observerable.Java文件:

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Observerable</span> {</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">registerObserver</span>(Observer o);    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">removeObserver</span>(Observer o);    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">notifyObservers</span>();}</a></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li></ul>

新建Observer.java文件:

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Observer</span> {</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">update</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> edition,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> cost);}</a></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li></ul>

Step 2.实现被观察者接口

新建MagazineData.java文件:

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">MagazineData</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Observerable</span> {</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> List<Observer> mObservers;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> edition;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> cost;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">MagazineData</span>() {        mObservers = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ArrayList<>();    }    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">registerObserver</span>(Observer o) {        mObservers.add(o);    }    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">removeObserver</span>(Observer o) {        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = mObservers.indexOf(o);        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(i >= <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)            mObservers.remove(i);    }    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">notifyObservers</span>() {        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < mObservers.size(); i++){            Observer observer = mObservers.get(i);            observer.update(edition, cost);        }    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">setInfomation</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> edition,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> cost){        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.edition = edition;        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.cost = cost;        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//信息更新完毕,通知所有观察者</span>        notifyObservers();    }}</a></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">18</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">19</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">20</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">21</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">22</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">23</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">24</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">25</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">26</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">27</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">28</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">29</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">30</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">31</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">32</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">33</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">34</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">35</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">36</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">37</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">38</a></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">18</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">19</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">20</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">21</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">22</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">23</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">24</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">25</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">26</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">27</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">28</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">29</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">30</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">31</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">32</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">33</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">34</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">35</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">36</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">37</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">38</a></li></ul>

Step 3.实现观察者接口

新建Customer.java文件:

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Customer</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Observer</span> {</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> String name;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> edition;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> cost;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">Customer</span>(String name){        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.name = name;    }    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">update</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> edition, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> cost) {        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.edition = edition;        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.cost = cost;        buy();    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">buy</span>(){        System.out.println(name+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"购买了第"</span>+edition+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"期的杂志,花费了"</span>+cost+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"元。"</span>);    }}</a></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">18</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">19</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">20</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">21</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">22</a></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">18</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">19</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">20</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">21</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">22</a></li></ul>

经过以上三个步骤,已经实现了观察者模式了,那么我们最后再编写一个测试类,来进行测试:

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Test</span> {</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>(String[] args) {        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建被观察者</span>        MagazineData magazine = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> MagazineData();        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建三个不同的观察者</span>        Observer customerA = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Customer(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"A"</span>);        Observer customerB = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Customer(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"B"</span>);        Observer customerC = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Customer(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"C"</span>);        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//将观察者注册到被观察者中</span>        magazine.registerObserver(customerA);        magazine.registerObserver(customerB);        magazine.registerObserver(customerC);        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//更新被观察者中的数据,当数据更新后,会自动通知所有已注册的观察者</span>        magazine.setInfomation(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>);    }}</a></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">18</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">19</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">20</a></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">18</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">19</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">20</a></li></ul>

运行,结果如下: 
结果

通过上面的例子,我们可以看出,MagazineData和Customer两者处于松耦合状态,甚至是零耦合状态,如果某一者需求改变了,需要进行代码的修改,这并不会涉及到另一者,这是非常良好的松耦合设计,只需要实现接口即可。

认识推模型和拉模型

通过上面的一个例子,我们简单实现了观察值模式,我们注意到在被观察者通知观察者的过程中,即在Observerable内调用Observer的update(int edition, float cost)方法时,我们把MagazineData的几个数据传递了进去,使得观察者能够从参数中得到数据,这就像从MagazineData中把数据“推”到了观察者中。这也就是推模型。与推模型相对的,假如MagazineData并不是把具体的数据传递过去,而是把自身的引用作为参数传递过去,那么观察者得到的是MagazineData实例的引用,那么我们可以直接通过一系列的get方法来获取数据,这也就是拉模型。下面,我们给推模型和拉模型下定义:

推模型:被观察者主动向观察者推送自身的信息,可以是全部信息或者是部分信息。

拉模型:被观察者通过把自身的引用传递给观察者,需要观察者自行通过该引用来获取相关的信息。

其实,我们上面实现的例子,就是使用了推模型,把MagazineData的两个成员变量推送给了观察者,如果要把以上例子修改为拉模型也很简单,在update()方法中,把参数修改为:update(Observerable observerable)即可,然后在观察者的实现类中,把得到的Observerable引用强制转换为具体的实现类型即可。

比较:推模型适用于提前知道观察者所需要的数据的情况,而拉模型由于把自身传递了过去,因此适用于大多数场景。

认识Java内置的观察者模式

上面我们自行实现了观察者模式,其实在Java中,它已经为我们准备了Observerable类以及Observer接口,只要继承或实现,就能快速实现我们的被观察者和观察者了,那么,我们学习下该内置的观察者模式。

首先,我们来看看java.util.Observerable类,这与我们上面自行实现的不同,它是一个类而不是一个接口,因此我们的被观察者要继承该类,另外,Observerable类已经帮我们实现了addObserver()、deleteObserver()、notifyObservers()等方法,因此我们在子类不需要再重写这些方法了,另外,该父类还提供了一个setChanged()方法,该方法用来标记状态已经改变的事实,因此要先调用该方法再调用notifyObservers()方法。其实,该类提供了两个notifyObservers()方法,一个有参数,一个无参数,而有参数的适用于推模型,无参数的适用于拉模型。 
java.util.Observer则是一个接口,与我们上面定义的基本一样。

那么,我们来利用Java内置的观察者模式结合拉模型来实现我们上面的小例子。 
新建JournalData.java:

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">JournalData</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Observable</span> {</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> edition;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> cost;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">setInfomation</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> edition,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> cost){        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.edition = edition;        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.cost = cost;        setChanged();        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//调用无参数的方法,使用拉模型</span>        notifyObservers();    }    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//提供get方法</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> <span class="hljs-title" style="box-sizing: border-box;">getEdition</span>(){        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> edition;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> <span class="hljs-title" style="box-sizing: border-box;">getCost</span>(){        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> cost;    }}</a></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">18</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">19</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">20</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">21</a></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">18</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">19</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">20</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">21</a></li></ul>

新建Consumer.java:

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Consumer</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Observer</span> {</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> String name;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> edition;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> cost;    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">Consumer</span>(String name){        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.name = name;    }    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">update</span>(Observable o, Object arg) {        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//判断o是否是JournalData的一个实例</span>        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(o <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">instanceof</span> JournalData){            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//强制转化为JournalData类型</span>            JournalData journalData = (JournalData) o;            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//拉取数据</span>            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.edition = journalData.getEdition();            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.cost = journalData.getCost();            buy();        }    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">buy</span>(){        System.out.println(name+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"购买了第"</span>+edition+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"期的杂志,花费了"</span>+cost+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"元。"</span>);    }}</a></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">18</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">19</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">20</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">21</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">22</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">23</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">24</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">25</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">26</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">27</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">28</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">29</a></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">18</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">19</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">20</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">21</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">22</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">23</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">24</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">25</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">26</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">27</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">28</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">29</a></li></ul>

测试类Test.java:

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>(String[] args) {        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建被观察者</span>        JournalData journal = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JournalData();        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建三个不同的观察者</span>        Consumer consumerA = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Consumer(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"A"</span>);        Consumer consumerB = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Consumer(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"B"</span>);        Consumer consumerC = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Consumer(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"C"</span>);        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//将观察者注册到被观察者中</span>        journal.addObserver(consumerA);        journal.addObserver(consumerB);        journal.addObserver(consumerC);        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//更新被观察者中的数据</span>        journal.setInfomation(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span>);    }}</a></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">1</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">2</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">3</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">4</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">5</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">6</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">7</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">8</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">9</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">10</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">11</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">12</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">13</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">14</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">15</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">16</a></li><li style="box-sizing: border-box; padding: 0px 5px;"><a target=_blank href="http://blog.csdn.net/a553181867/article/details/52454178" style="font-family: "microsoft yahei"; font-size: 18px; font-weight: bold; text-decoration: none; color: rgb(0, 0, 0);">17</a></li></ul>

运行结果如下所示:

结果

经过上面的几个例子,相信大家对观察者模式有了一个比较深刻的认识了,最后我们再说说观察者模式可以用在什么地方。比如我们有两个对象,一个对象依赖于另一个对象的变化而变化,此时我们可以将这两个对象抽象出来,做成接口,利用观察者模式来进行解耦,又或者,当一个对象发生变化的时候,需要通知别的对象来做出改变,但又不知道这样的对象有多少个,此时利用观察者模式非常合适。

最后,谢谢大家的阅读~

0 0