http://blog.csdn.net/a553181867/article/details/52454178
http://blog.csdn.net/a553181867/article/details/52454178
http://blog.csdn.net/a553181867/article/details/52454178
前言
定义
那么,我们已经知道了观察者和被观察者的概念,但我们还不知道它们两个是怎样联系起来的,接下来我们正要解决这个问题,为了更好地理解,我们先来看看观察者模式的UML类图。
UML类图
Observer:观察者接口,规定了update()方法,当被观察者调用notifyObservers()方法时,观察者的update()方法会被回调。
ConcreteObserver:观察者,实现了update()方法。
那么了解了以上各部分的含义后,应该不难得出观察者模式的实现过程了,那么我们接下来看看实现观察者模式的一般过程。
实现步骤
A Sample
Step 1.建立接口
<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>
<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.实现被观察者接口
<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.实现观察者接口
<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>
认识推模型和拉模型
推模型:被观察者主动向观察者推送自身的信息,可以是全部信息或者是部分信息。
拉模型:被观察者通过把自身的引用传递给观察者,需要观察者自行通过该引用来获取相关的信息。
比较:推模型适用于提前知道观察者所需要的数据的情况,而拉模型由于把自身传递了过去,因此适用于大多数场景。
认识Java内置的观察者模式
那么,我们来利用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>
<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>
<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>