理解软件设计模式

 理解软件设计模式

设计模式可以帮助消除冗余代码。学习如何利用 Java 使用单例模式、工厂模式和观察者模式。

如果你是一名正在致力于计算机科学或者相关学科的程序员或者学生,很快,你将会遇到一条术语 “软件设计模式software design pattern”。根据维基百科,“软件设计模式是在平常的软件设计工作中所遭遇的问题的一种通用的、可重复使用的解决方案”。我对该定义的理解是:当在从事于一个编码项目时,你经常会思考,“嗯,这里貌似是冗余代码,我觉得是否能改变这些代码使之更灵活和便于修改?”因此,你会开始考虑怎样分割那些保持不变的内容和需要经常改变的内容。

设计模式是一种通过分割那些保持不变的部分和经常变化的部分,让你的代码更容易修改的方法。

不出意外的话,每个从事编程项目的人都可能会有同样的思考。特别是那些工业级别的项目,在那里通常工作着数十甚至数百名开发者;协作过程表明必须有一些标准和规则来使代码更加优雅并适应变化。这就是为什么我们有了 面向对象编程(OOP)和 软件框架工具。设计模式有点类似于 OOP,但它通过将变化视为自然开发过程的一部分而进一步发展。基本上,设计模式利用了一些 OOP 的思想,比如抽象和接口,但是专注于改变的过程。

当你开始开发项目时,你经常会听到这样一个术语重构,它意味着通过改变代码使它变得更优雅和可复用;这就是设计模式耀眼的地方。当你处理现有代码时(无论是由其他人构建还是你自己过去构建的),了解设计模式可以帮助你以不同的方式看待事物,你将发现问题以及改进代码的方法。

有很多种设计模式,其中单例模式、工厂模式和观察者模式三种最受欢迎,在这篇文章中我将会一一介绍它们。

如何遵循本指南

无论你是一位有经验的编程工作者还是一名刚刚接触的新手,我想让这篇教程让每个人都很容易理解。设计模式概念并不容易理解,减少开始旅程时的学习曲线始终是首要任务。因此,除了这篇带有图表和代码片段的文章外,我还创建了一个 GitHub 仓库,你可以克隆仓库并在你的电脑上运行这些代码来实现这三种设计模式。你也可以观看我创建的 YouTube视频

必要条件

如果你只是想了解一般的设计模式思想,则无需克隆示例项目或安装任何工具。但是,如果要运行示例代码,你需要安装以下工具:

  • Java 开发套件(JDK):我强烈建议使用 OpenJDK
  • Apache Maven:这个简单的项目使用 Apache Maven 构建;好的是许多 IDE 自带了Maven。
  • 交互式开发编辑器(IDE):我使用 社区版 IntelliJ,但是你也可以使用 Eclipse IDE 或者其他你喜欢的 Java IDE。
  • Git:如果你想克隆这个工程,你需要 Git 客户端。

安装好 Git 后运行下列命令克隆这个工程:

  1. <span class="kwd">git</span><span class="pln"> </span><span class="kwd">clone</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//github.com/bryantson/OpensourceDotComDemos.git</span>

然后在你喜欢的 IDE 中,你可以将 TopDesignPatterns 仓库中的代码作为 Apache Maven 项目导入。

我使用的是 Java,但你也可以使用支持抽象原则的任何编程语言来实现设计模式。

单例模式:避免每次创建一个对象

单例模式singleton pattern是非常流行的设计模式,它的实现相对来说很简单,因为你只需要一个类。然而,许多开发人员争论单例设计模式的是否利大于弊,因为它缺乏明显的好处并且容易被滥用。很少有开发人员直接实现单例;相反,像 Spring Framework 和 Google Guice 等编程框架内置了单例设计模式的特性。

但是了解单例模式仍然有巨大的用处。单例模式确保一个类仅创建一次且提供了一个对它的全局访问点。

单例模式:确保仅创建一个实例且避免在同一个项目中创建多个实例。

下面这幅图展示了典型的类对象创建过程。当客户端请求创建一个对象时,构造函数会创建或者实例化一个对象并调用方法返回这个类给调用者。但是每次请求一个对象都会发生这样的情况:构造函数被调用,一个新的对象被创建并且它返回了一个独一无二的对象。我猜面向对象语言的创建者有每次都创建一个新对象的理由,但是单例过程的支持者说这是冗余的且浪费资源。

理解软件设计模式

Normal class instantiation

下面这幅图使用单例模式创建对象。这里,构造函数仅当对象首次通过调用预先设计好的 getInstance() 方法时才会被调用。这通常通过检查该值是否为 null 来完成,并且这个对象被作为私有变量保存在单例类的内部。下次 getInstance() 被调用时,这个类会返回第一次被创建的对象。而没有新的对象产生;它只是返回旧的那一个。

理解软件设计模式

Singleton pattern instantiation

下面这段代码展示了创建单例模式最简单的方法:

  1. <span class="kwd">package</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">opensource</span><span class="pun">.</span><span class="pln">demo</span><span class="pun">.</span><span class="pln">singleton</span><span class="pun">;</span>
  2.  
  3. <span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">OpensourceSingleton</span><span class="pln"> </span><span class="pun">{</span>
  4.  
  5. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="typ">OpensourceSingleton</span><span class="pln"> uniqueInstance</span><span class="pun">;</span>
  6.  
  7. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">OpensourceSingleton</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span>
  8. <span class="pln">    </span><span class="pun">}</span>
  9.  
  10. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="typ">OpensourceSingleton</span><span class="pln"> getInstance</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span>
  11. <span class="pln">        </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">uniqueInstance </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  12. <span class="pln">            uniqueInstance </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">OpensourceSingleton</span><span class="pun">();</span>
  13. <span class="pln">        </span><span class="pun">}</span>
  14. <span class="pln">        </span><span class="kwd">return</span><span class="pln"> uniqueInstance</span><span class="pun">;</span>
  15. <span class="pln">    </span><span class="pun">}</span>
  16.  
  17. <span class="pun">}</span>

在调用方,这里展示了如何调用单例类来获取对象:

  1. <span class="typ">Opensource</span><span class="pln"> newObject </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Opensource</span><span class="pun">.</span><span class="pln">getInstance</span><span class="pun">();</span>

这段代码很好的验证了单例模式的思想:

  1. 当 getInstance() 被调用时,它通过检查 null 值来检查对象是否已经被创建。
  2. 如果值为 null,它会创建一个新对象并把它保存到私有域,返回这个对象给调用者。否则直接返回之前被创建的对象。

单例模式实现的主要问题是它忽略了并行进程。当多个进程使用线程同时访问资源时,这个问题就产生了。对于这种情况有对应的解决方案,它被称为双重检查锁,用于多线程安全,如下所示:

  1. <span class="kwd">package</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">opensource</span><span class="pun">.</span><span class="pln">demo</span><span class="pun">.</span><span class="pln">singleton</span><span class="pun">;</span>
  2.  
  3. <span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ImprovedOpensourceSingleton</span><span class="pln"> </span><span class="pun">{</span>
  4.  
  5. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">volatile</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="typ">ImprovedOpensourceSingleton</span><span class="pln"> uniqueInstance</span><span class="pun">;</span>
  6.  
  7. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">ImprovedOpensourceSingleton</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{}</span>
  8.  
  9. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="typ">ImprovedOpensourceSingleton</span><span class="pln"> getInstance</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span>
  10. <span class="pln">        </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">uniqueInstance </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  11. <span class="pln">            synchronized </span><span class="pun">(</span><span class="typ">ImprovedOpensourceSingleton</span><span class="pun">.</span><span class="kwd">class</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  12. <span class="pln">                </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">uniqueInstance </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  13. <span class="pln">                    uniqueInstance </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">ImprovedOpensourceSingleton</span><span class="pun">();</span>
  14. <span class="pln">                </span><span class="pun">}</span>
  15. <span class="pln">            </span><span class="pun">}</span>
  16. <span class="pln">        </span><span class="pun">}</span>
  17. <span class="pln">        </span><span class="kwd">return</span><span class="pln"> uniqueInstance</span><span class="pun">;</span>
  18. <span class="pln">    </span><span class="pun">}</span>
  19.  
  20. <span class="pun">}</span>

再强调一下前面的观点,确保只有在你认为这是一个安全的选择时才直接实现你的单例模式。最好的方法是通过使用一个制作精良的编程框架来利用单例功能。

工厂模式:将对象创建委派给工厂类以隐藏创建逻辑

工厂模式factory pattern是另一种众所周知的设计模式,但是有一小点复杂。实现工厂模式的方法有很多,而下列的代码示例为最简单的实现方式。为了创建对象,工厂模式定义了一个接口,让它的子类去决定实例化哪一个类。

工厂模式:将对象创建委派给工厂类,因此它能隐藏创建逻辑。

下列的图片展示了最简单的工厂模式是如何实现的。

理解软件设计模式

Factory pattern

客户端请求工厂类创建类型为 x 的某个对象,而不是客户端直接调用对象创建。根据其类型,工厂模式决定要创建和返回的对象。

在下列代码示例中,OpensourceFactory 是工厂类实现,它从调用者那里获取类型并根据该输入值决定要创建和返回的对象:

  1. <span class="kwd">package</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">opensource</span><span class="pun">.</span><span class="pln">demo</span><span class="pun">.</span><span class="pln">factory</span><span class="pun">;</span>
  2.  
  3. <span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">OpensourceFactory</span><span class="pln"> </span><span class="pun">{</span>
  4.  
  5. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">OpensourceJVMServers</span><span class="pln"> getServerByVendor</span><span class="pun">([</span><span class="typ">String</span><span class="pun">][</span><span class="lit">18</span><span class="pun">]</span><span class="pln"> name</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  6. <span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">name</span><span class="pun">.</span><span class="pln">equals</span><span class="pun">(</span><span class="str">"Apache"</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span>
  7. <span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Tomcat</span><span class="pun">();</span>
  8. <span class="pln">        </span><span class="pun">}</span>
  9. <span class="pln">        </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">name</span><span class="pun">.</span><span class="pln">equals</span><span class="pun">(</span><span class="str">"Eclipse"</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span>
  10. <span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Jetty</span><span class="pun">();</span>
  11. <span class="pln">        </span><span class="pun">}</span>
  12. <span class="pln">        </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">name</span><span class="pun">.</span><span class="pln">equals</span><span class="pun">(</span><span class="str">"RedHat"</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span>
  13. <span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">WildFly</span><span class="pun">();</span>
  14. <span class="pln">        </span><span class="pun">}</span>
  15. <span class="pln">        </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span>
  16. <span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span>
  17. <span class="pln">        </span><span class="pun">}</span>
  18. <span class="pln">    </span><span class="pun">}</span>
  19. <span class="pun">}</span>

OpenSourceJVMServer 是一个 100% 的抽象类(即接口类),它指示要实现的是什么,而不是怎样实现:

  1. <span class="kwd">package</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">opensource</span><span class="pun">.</span><span class="pln">demo</span><span class="pun">.</span><span class="pln">factory</span><span class="pun">;</span>
  2.  
  3. <span class="kwd">public</span><span class="pln"> </span><span class="kwd">interface</span><span class="pln"> </span><span class="typ">OpensourceJVMServers</span><span class="pln"> </span><span class="pun">{</span>
  4. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> startServer</span><span class="pun">();</span>
  5. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> stopServer</span><span class="pun">();</span>
  6. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="pun">[</span><span class="typ">String</span><span class="pun">][</span><span class="lit">18</span><span class="pun">]</span><span class="pln"> getName</span><span class="pun">();</span>
  7. <span class="pun">}</span>

这是一个 OpensourceJVMServers 类的实现示例。当 RedHat 被作为类型传递给工厂类,WildFly 服务器将被创建:

  1. <span class="kwd">package</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">opensource</span><span class="pun">.</span><span class="pln">demo</span><span class="pun">.</span><span class="pln">factory</span><span class="pun">;</span>
  2.  
  3. <span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">WildFly</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><span class="typ">OpensourceJVMServers</span><span class="pln"> </span><span class="pun">{</span>
  4. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> startServer</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span>
  5. <span class="pln">        </span><span class="pun">[</span><span class="typ">System</span><span class="pun">][</span><span class="lit">19</span><span class="pun">].</span><span class="pln">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Starting WildFly Server..."</span><span class="pun">);</span>
  6. <span class="pln">    </span><span class="pun">}</span>
  7.  
  8. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> stopServer</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span>
  9. <span class="pln">        </span><span class="pun">[</span><span class="typ">System</span><span class="pun">][</span><span class="lit">19</span><span class="pun">].</span><span class="pln">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Shutting Down WildFly Server..."</span><span class="pun">);</span>
  10. <span class="pln">    </span><span class="pun">}</span>
  11.  
  12. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="pun">[</span><span class="typ">String</span><span class="pun">][</span><span class="lit">18</span><span class="pun">]</span><span class="pln"> getName</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span>
  13. <span class="pln">        </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"WildFly"</span><span class="pun">;</span>
  14. <span class="pln">    </span><span class="pun">}</span>
  15. <span class="pun">}</span>

观察者模式:订阅主题并获取相关更新的通知

最后是观察者模式observer pattern。像单例模式那样,很少有专业的程序员直接实现观察者模式。但是,许多消息队列和数据服务实现都借用了观察者模式的概念。观察者模式在对象之间定义了一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都将被自动地通知和更新。

观察者模式:如果有更新,那么订阅了该话题/主题的客户端将被通知。

理解观察者模式的最简单方法是想象一个邮件列表,你可以在其中订阅任何主题,无论是开源、技术、名人、烹饪还是您感兴趣的任何其他内容。每个主题维护者一个它的订阅者列表,在观察者模式中它们相当于观察者。当某一个主题更新时,它所有的订阅者(观察者)都将被通知这次改变。并且订阅者总是能取消某一个主题的订阅。

如下图所示,客户端可以订阅不同的主题并添加观察者以获得最新信息的通知。因为观察者不断的监听着这个主题,这个观察者会通知客户端任何发生的改变。

理解软件设计模式

Observer pattern

让我们来看看观察者模式的代码示例,从主题/话题类开始:

  1. <span class="kwd">package</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">opensource</span><span class="pun">.</span><span class="pln">demo</span><span class="pun">.</span><span class="pln">observer</span><span class="pun">;</span>
  2.  
  3. <span class="kwd">public</span><span class="pln"> </span><span class="kwd">interface</span><span class="pln"> </span><span class="typ">Topic</span><span class="pln"> </span><span class="pun">{</span>
  4.  
  5. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> addObserver</span><span class="pun">([</span><span class="typ">Observer</span><span class="pun">][</span><span class="lit">22</span><span class="pun">]</span><span class="pln"> observer</span><span class="pun">);</span>
  6. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> deleteObserver</span><span class="pun">([</span><span class="typ">Observer</span><span class="pun">][</span><span class="lit">22</span><span class="pun">]</span><span class="pln"> observer</span><span class="pun">);</span>
  7. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> notifyObservers</span><span class="pun">();</span>
  8. <span class="pun">}</span>

这段代码描述了一个为不同的主题去实现已定义方法的接口。注意一个观察者如何被添加、移除和通知的。

这是一个主题的实现示例:

  1. <span class="kwd">package</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">opensource</span><span class="pun">.</span><span class="pln">demo</span><span class="pun">.</span><span class="pln">observer</span><span class="pun">;</span>
  2.  
  3. <span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">util</span><span class="pun">.</span><span class="typ">List</span><span class="pun">;</span>
  4. <span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">util</span><span class="pun">.</span><span class="typ">ArrayList</span><span class="pun">;</span>
  5.  
  6. <span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Conference</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><span class="typ">Topic</span><span class="pln"> </span><span class="pun">{</span>
  7. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">List</span><span class="pun">&</span><span class="pln">lt</span><span class="pun">;</span><span class="typ">Observer</span><span class="pun">&</span><span class="pln">gt</span><span class="pun">;</span><span class="pln"> listObservers</span><span class="pun">;</span>
  8. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> totalAttendees</span><span class="pun">;</span>
  9. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> totalSpeakers</span><span class="pun">;</span>
  10. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="pun">[</span><span class="typ">String</span><span class="pun">][</span><span class="lit">18</span><span class="pun">]</span><span class="pln"> nameEvent</span><span class="pun">;</span>
  11.  
  12. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">Conference</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span>
  13. <span class="pln">        listObservers </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">ArrayList</span><span class="pun">&</span><span class="pln">lt</span><span class="pun">;</span><span class="typ">Observer</span><span class="pun">&</span><span class="pln">gt</span><span class="pun">;();</span>
  14. <span class="pln">    </span><span class="pun">}</span>
  15.  
  16. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> addObserver</span><span class="pun">([</span><span class="typ">Observer</span><span class="pun">][</span><span class="lit">22</span><span class="pun">]</span><span class="pln"> observer</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  17. <span class="pln">        listObservers</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="pln">observer</span><span class="pun">);</span>
  18. <span class="pln">    </span><span class="pun">}</span>
  19.  
  20. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> deleteObserver</span><span class="pun">([</span><span class="typ">Observer</span><span class="pun">][</span><span class="lit">22</span><span class="pun">]</span><span class="pln"> observer</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  21. <span class="pln">        </span><span class="kwd">int</span><span class="pln"> i </span><span class="pun">=</span><span class="pln"> listObservers</span><span class="pun">.</span><span class="pln">indexOf</span><span class="pun">(</span><span class="pln">observer</span><span class="pun">);</span>
  22. <span class="pln">        </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">i </span><span class="pun">&</span><span class="pln">gt</span><span class="pun">;=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  23. <span class="pln">            listObservers</span><span class="pun">.</span><span class="pln">remove</span><span class="pun">(</span><span class="pln">i</span><span class="pun">);</span>
  24. <span class="pln">        </span><span class="pun">}</span>
  25. <span class="pln">    </span><span class="pun">}</span>
  26.  
  27. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> notifyObservers</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span>
  28. <span class="pln">        </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> nObservers </span><span class="pun">=</span><span class="pln"> listObservers</span><span class="pun">.</span><span class="kwd">size</span><span class="pun">();</span><span class="pln"> i </span><span class="pun">&</span><span class="pln">lt</span><span class="pun">;</span><span class="pln"> nObservers</span><span class="pun">;</span><span class="pln"> </span><span class="pun">++</span><span class="pln"> i</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  29. <span class="pln">            </span><span class="pun">[</span><span class="typ">Observer</span><span class="pun">][</span><span class="lit">22</span><span class="pun">]</span><span class="pln"> observer </span><span class="pun">=</span><span class="pln"> listObservers</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="pln">i</span><span class="pun">);</span>
  30. <span class="pln">            observer</span><span class="pun">.</span><span class="pln">update</span><span class="pun">(</span><span class="pln">totalAttendees</span><span class="pun">,</span><span class="pln">totalSpeakers</span><span class="pun">,</span><span class="pln">nameEvent</span><span class="pun">);</span>
  31. <span class="pln">        </span><span class="pun">}</span>
  32. <span class="pln">    </span><span class="pun">}</span>
  33.  
  34. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setConferenceDetails</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> totalAttendees</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> totalSpeakers</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="typ">String</span><span class="pun">][</span><span class="lit">18</span><span class="pun">]</span><span class="pln"> nameEvent</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  35. <span class="pln">        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">totalAttendees </span><span class="pun">=</span><span class="pln"> totalAttendees</span><span class="pun">;</span>
  36. <span class="pln">        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">totalSpeakers </span><span class="pun">=</span><span class="pln"> totalSpeakers</span><span class="pun">;</span>
  37. <span class="pln">        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">nameEvent </span><span class="pun">=</span><span class="pln"> nameEvent</span><span class="pun">;</span>
  38. <span class="pln">        notifyObservers</span><span class="pun">();</span>
  39. <span class="pln">    </span><span class="pun">}</span>
  40. <span class="pun">}</span>

这段代码定义了一个特定主题的实现。当发生改变时,这个实现调用它自己的方法。注意这将获取观察者的数量,它以列表方式存储,并且可以通知和维护观察者。

这是一个观察者类:

  1. <span class="kwd">package</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">opensource</span><span class="pun">.</span><span class="pln">demo</span><span class="pun">.</span><span class="pln">observer</span><span class="pun">;</span>
  2.  
  3. <span class="kwd">public</span><span class="pln"> </span><span class="kwd">interface</span><span class="pln"> </span><span class="pun">[</span><span class="typ">Observer</span><span class="pun">][</span><span class="lit">22</span><span class="pun">]</span><span class="pln"> </span><span class="pun">{</span>
  4. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> update</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> totalAttendees</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> totalSpeakers</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="typ">String</span><span class="pun">][</span><span class="lit">18</span><span class="pun">]</span><span class="pln"> nameEvent</span><span class="pun">);</span>
  5. <span class="pun">}</span>

这个类定义了一个接口,不同的观察者可以实现该接口以执行特定的操作。

例如,实现了该接口的观察者可以在会议上打印出与会者和发言人的数量:

  1. <span class="kwd">package</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">opensource</span><span class="pun">.</span><span class="pln">demo</span><span class="pun">.</span><span class="pln">observer</span><span class="pun">;</span>
  2.  
  3. <span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MonitorConferenceAttendees</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><span class="pun">[</span><span class="typ">Observer</span><span class="pun">][</span><span class="lit">22</span><span class="pun">]</span><span class="pln"> </span><span class="pun">{</span>
  4. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> totalAttendees</span><span class="pun">;</span>
  5. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> totalSpeakers</span><span class="pun">;</span>
  6. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="pun">[</span><span class="typ">String</span><span class="pun">][</span><span class="lit">18</span><span class="pun">]</span><span class="pln"> nameEvent</span><span class="pun">;</span>
  7. <span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">Topic</span><span class="pln"> topic</span><span class="pun">;</span>
  8.  
  9. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">MonitorConferenceAttendees</span><span class="pun">(</span><span class="typ">Topic</span><span class="pln"> topic</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  10. <span class="pln">        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">topic </span><span class="pun">=</span><span class="pln"> topic</span><span class="pun">;</span>
  11. <span class="pln">        topic</span><span class="pun">.</span><span class="pln">addObserver</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">);</span>
  12. <span class="pln">    </span><span class="pun">}</span>
  13.  
  14. <span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> update</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> totalAttendees</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> totalSpeakers</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="typ">String</span><span class="pun">][</span><span class="lit">18</span><span class="pun">]</span><span class="pln"> nameEvent</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span>
  15. <span class="pln">        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">totalAttendees </span><span class="pun">=</span><span class="pln"> totalAttendees</span><span class="pun">;</span>
  16. <span class="pln">        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">totalSpeakers </span><span class="pun">=</span><span class="pln"> totalSpeakers</span><span class="pun">;</span>
  17. <span class="pln">        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">nameEvent </span><span class="pun">=</span><span class="pln"> nameEvent</span><span class="pun">;</span>
  18. <span class="pln">        printConferenceInfo</span><span class="pun">();</span>
  19. <span class="pln">    </span><span class="pun">}

相关推荐