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

2024-06-19 13:08

本文主要是介绍学习、探究Java设计模式——观察者模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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>

运行结果如下所示:

结果

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

最后,谢谢大家的阅读~

这篇关于学习、探究Java设计模式——观察者模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1075117

相关文章

51单片机学习记录———定时器

文章目录 前言一、定时器介绍二、STC89C52定时器资源三、定时器框图四、定时器模式五、定时器相关寄存器六、定时器练习 前言 一个学习嵌入式的小白~ 有问题评论区或私信指出~ 提示:以下是本篇文章正文内容,下面案例可供参考 一、定时器介绍 定时器介绍:51单片机的定时器属于单片机的内部资源,其电路的连接和运转均在单片机内部完成。 定时器作用: 1.用于计数系统,可

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

Java五子棋之坐标校正

上篇针对了Java项目中的解构思维,在这篇内容中我们不妨从整体项目中拆解拿出一个非常重要的五子棋逻辑实现:坐标校正,我们如何使漫无目的鼠标点击变得有序化和可控化呢? 目录 一、从鼠标监听到获取坐标 1.MouseListener和MouseAdapter 2.mousePressed方法 二、坐标校正的具体实现方法 1.关于fillOval方法 2.坐标获取 3.坐标转换 4.坐

Spring Cloud:构建分布式系统的利器

引言 在当今的云计算和微服务架构时代,构建高效、可靠的分布式系统成为软件开发的重要任务。Spring Cloud 提供了一套完整的解决方案,帮助开发者快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器等)。本文将探讨 Spring Cloud 的定义、核心组件、应用场景以及未来的发展趋势。 什么是 Spring Cloud Spring Cloud 是一个基于 Spring

[word] word设置上标快捷键 #学习方法#其他#媒体

word设置上标快捷键 办公中,少不了使用word,这个是大家必备的软件,今天给大家分享word设置上标快捷键,希望在办公中能帮到您! 1、添加上标 在录入一些公式,或者是化学产品时,需要添加上标内容,按下快捷键Ctrl+shift++就能将需要的内容设置为上标符号。 word设置上标快捷键的方法就是以上内容了,需要的小伙伴都可以试一试呢!

AssetBundle学习笔记

AssetBundle是unity自定义的资源格式,通过调用引擎的资源打包接口对资源进行打包成.assetbundle格式的资源包。本文介绍了AssetBundle的生成,使用,加载,卸载以及Unity资源更新的一个基本步骤。 目录 1.定义: 2.AssetBundle的生成: 1)设置AssetBundle包的属性——通过编辑器界面 补充:分组策略 2)调用引擎接口API

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

java8的新特性之一(Java Lambda表达式)

1:Java8的新特性 Lambda 表达式: 允许以更简洁的方式表示匿名函数(或称为闭包)。可以将Lambda表达式作为参数传递给方法或赋值给函数式接口类型的变量。 Stream API: 提供了一种处理集合数据的流式处理方式,支持函数式编程风格。 允许以声明性方式处理数据集合(如List、Set等)。提供了一系列操作,如map、filter、reduce等,以支持复杂的查询和转

大学湖北中医药大学法医学试题及答案,分享几个实用搜题和学习工具 #微信#学习方法#职场发展

今天分享拥有拍照搜题、文字搜题、语音搜题、多重搜题等搜题模式,可以快速查找问题解析,加深对题目答案的理解。 1.快练题 这是一个网站 找题的网站海量题库,在线搜题,快速刷题~为您提供百万优质题库,直接搜索题库名称,支持多种刷题模式:顺序练习、语音听题、本地搜题、顺序阅读、模拟考试、组卷考试、赶快下载吧! 2.彩虹搜题 这是个老公众号了 支持手写输入,截图搜题,详细步骤,解题必备

如何开启和关闭3GB模式

https://jingyan.baidu.com/article/4d58d5414dfc2f9dd4e9c082.html