Java中如何使用Observer模式-創(chuàng)新互聯(lián)

小編給大家分享一下Java中如何使用Observer模式,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都一家集口碑和實(shí)力的網(wǎng)站建設(shè)服務(wù)商,擁有專業(yè)的企業(yè)建站團(tuán)隊(duì)和靠譜的建站技術(shù),十多年企業(yè)及個(gè)人網(wǎng)站建設(shè)經(jīng)驗(yàn) ,為成都上千客戶提供網(wǎng)頁(yè)設(shè)計(jì)制作,網(wǎng)站開發(fā),企業(yè)網(wǎng)站制作建設(shè)等服務(wù),包括成都營(yíng)銷型網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),同時(shí)也為不同行業(yè)的客戶提供成都做網(wǎng)站、成都網(wǎng)站制作的服務(wù),包括成都電商型網(wǎng)站制作建設(shè),裝修行業(yè)網(wǎng)站制作建設(shè),傳統(tǒng)機(jī)械行業(yè)網(wǎng)站建設(shè),傳統(tǒng)農(nóng)業(yè)行業(yè)網(wǎng)站制作建設(shè)。在成都做網(wǎng)站,選網(wǎng)站制作建設(shè)服務(wù)商就選創(chuàng)新互聯(lián)。

Java Q&A: 使用Observer模式

A:我想在自己的Java程序中使用Observer模式?;诖耍芊裉峁┠承┦纠a來(lái)演示如何去做?

Q:正如面向?qū)ο缶幊逃兄诖a復(fù)用一樣,設(shè)計(jì)模式可以促進(jìn)設(shè)計(jì)復(fù)用。確實(shí),設(shè)計(jì)模式可以讓你復(fù)用那些正確的、成熟的設(shè)計(jì)。但近來(lái),批評(píng)設(shè)計(jì)模式的聲音越來(lái)越多。批評(píng)者指出,缺乏經(jīng)驗(yàn)的開發(fā)者會(huì)很容易地掉進(jìn) "模式陷阱" 之中。

模式陷阱讓缺乏經(jīng)驗(yàn)的開發(fā)者迷失了方向。因而,他們?cè)谔幚韱?wèn)題時(shí)不是去尋找可能存在的最好方案,而把最終目標(biāo)集中在盡可能地實(shí)現(xiàn)更多的設(shè)計(jì)模式之上。在一些人看來(lái),使用設(shè)計(jì)模式好象必然會(huì)帶來(lái)良好的設(shè)計(jì)。按照這個(gè)邏輯,只要大量使用設(shè)計(jì)模式,就必然為你產(chǎn)生一個(gè)優(yōu)秀的設(shè)計(jì)!然而現(xiàn)實(shí)中,這一觀點(diǎn)卻導(dǎo)致出許多毫無(wú)意義的設(shè)計(jì)--即使這個(gè)設(shè)計(jì)采用了多個(gè)設(shè)計(jì)模式??磥?lái)很遺憾,設(shè)計(jì)模式并不能保證良好的設(shè)計(jì)。

要想在設(shè)計(jì)中正確地運(yùn)用一個(gè)設(shè)計(jì)模式,必須確保三個(gè)條件:

1. 弄清你的問(wèn)題

2. 了解這個(gè)模式

3. 理解這個(gè)模式如何解決你的問(wèn)題

首先,最重要的是條件1。如果不能完全地弄清你要解決的問(wèn)題,何談運(yùn)用模式?

還要知道條件2:必須完全了解你想運(yùn)用的模式。不了解它怎么能運(yùn)用它?更重要的是,連一個(gè)模式做什么都不知道,又如何想到去用它?

最后一點(diǎn),如果你不能清楚地說(shuō)出模式將怎樣解決你的問(wèn)題(為什么這個(gè)模式合適),那還是放棄它。僅僅為了用模式本身而去用它,就會(huì)掉進(jìn)模式陷阱之中。

我并不是在說(shuō)提這個(gè)問(wèn)題的讀者也一定會(huì)掉進(jìn)模式陷阱。但從提問(wèn)的表達(dá)上來(lái)看,它很容易誤導(dǎo)一些開發(fā)者去理解設(shè)計(jì)模式。我對(duì)這個(gè)提問(wèn)的理解是,這位讀者應(yīng)該清楚自己需要解決的問(wèn)題,也了解Observer模式,他/她只是不知道如何用Java來(lái)實(shí)現(xiàn)。

在給出一個(gè)Java示例之前,為了有助于其他讀者也能理解,先簡(jiǎn)要介紹一下Observer模式。

簡(jiǎn)單來(lái)說(shuō),Observer模式讓一個(gè)對(duì)象(觀察者,Observer)去監(jiān)視另一個(gè)對(duì)象(目標(biāo),Subject);它使得目標(biāo)和觀察者之間建立一種 "發(fā)布--訂閱"(publish-subscribe )的關(guān)系。通過(guò)Observer模式,觀察者可以向目標(biāo)登記,表明自己要從目標(biāo)接收事件。目標(biāo)需要向觀察者通知事件時(shí),只是簡(jiǎn)單地將事件發(fā)給每一個(gè)觀察者。

例如,有一個(gè)基于某種數(shù)據(jù)模型的電子表格。只要數(shù)據(jù)模型發(fā)生變化,電子表格就需要更新表格單元以及內(nèi)嵌的圖表。這個(gè)例子中,目標(biāo)是數(shù)據(jù)模型,觀察者是表格單元和圖表。當(dāng)觀察者接收到數(shù)據(jù)模型已經(jīng)變化的通知時(shí),它們就更新自己。

Observer模式的好處是:它解除了觀察者和目標(biāo)之間的耦合關(guān)系。目標(biāo)不需要知道它的觀察者的任何信息。相反,目標(biāo)只是允許觀察者訂閱事件。當(dāng)目標(biāo)產(chǎn)生一個(gè)事件時(shí),它簡(jiǎn)單地將事件傳給每一個(gè)觀察者。

看看下面的Java示例:

public interface Subject {
  public void addObserver( Observer o );
  public void removeObserver( Observer o );
}

上面的代碼中,Subject接口定義了兩個(gè)方法(method),每個(gè)Subject都必須實(shí)現(xiàn)它們,以使Observer可以在Subject中增加或刪除自身。

public interface Observer {
  public void update( Subject o );
}

Observer接口(如上)列出了一個(gè)方法(method),每個(gè)Observer都必須實(shí)現(xiàn)它,以使Subject可以發(fā)送更新消息給Observer。

下面看看Subject的一個(gè)簡(jiǎn)單的實(shí)現(xiàn)--IntegerDataBag:

import java.util.ArrayList;
import java.util.Iterator;

public class IntegerDataBag implements Subject {

  private ArrayList list = new ArrayList();
  private ArrayList observers = new ArrayList();

  public void add( Integer i ) {
  list.add( i );
  notifyObservers();
  }

  public Iterator iterator() {
  return list.iterator();
  }

  public Integer remove( int index ) {
  if( index < list.size() ) {
  Integer i = (Integer) list.remove( index );
  notifyObservers();
  return i;
  }
  return null;
  }

  public void addObserver( Observer o ) {
  observers.add( o );
  }

  public void removeObserver( Observer o ) {
  observers.remove( o );
  }

  private void notifyObservers() {
  // loop through and notify each observer
  Iterator i = observers.iterator();
  while( i.hasNext() ) {
  Observer o = ( Observer ) i.next();
  o.update( this );
  }
  }
}

IntegerDataBag適用于使用Integer的場(chǎng)合。IntegerDataBag也允許Observer增加和刪除它們自身。

再看看兩個(gè)Observer的實(shí)現(xiàn)--IntegerAdder和IntegerPrinter:

import java.util.Iterator;

public class IntegerAdder implements Observer {

  private IntegerDataBag bag;

  public IntegerAdder( IntegerDataBag bag ) {
  this.bag = bag; 
  bag.addObserver( this );
  }

  public void update( Subject o ) {
  if( o == bag ) {
  System.out.println( "The contents of the IntegerDataBag have changed." );
  int counter = 0;
  Iterator i = bag.iterator();
  while( i.hasNext() ) {
  Integer integer = ( Integer ) i.next();
  counter+=integer.intValue();
  }
  System.out.println( "The new sum of the integers is: " + counter );
  }
  }

}

import java.util.Iterator;

public class IntegerPrinter implements Observer {

  private IntegerDataBag bag;

  public IntegerPrinter( IntegerDataBag bag ) {
  this.bag = bag; 
  bag.addObserver( this );
  }

  public void update( Subject o ) {
  if( o == bag ) {
  System.out.println( "The contents of the IntegerDataBag have changed." );
  System.out.println( "The new contents of the IntegerDataBag contains:" );
  Iterator i = bag.iterator();
  while( i.hasNext() ) {
  System.out.println( i.next() );
  }
  }
  }

}

IntegerAdder和IntegerPrinter將自己作為觀察者增加到IntegerDataBag。當(dāng)IntegerAdder接收到一條更新消息時(shí),它先統(tǒng)計(jì)bag中的總數(shù),然后顯示結(jié)果。同樣,當(dāng)IntegerPrinter接收到一條更新消息時(shí),它打印出bag中的Interger。

下面是一個(gè)簡(jiǎn)單的main(),它使用了上面的幾個(gè)類:

public class driver {
  public static void main( String [] args ) {
  Integer i1 = new Integer( 1 ); Integer i2 = new Integer( 2 );
  Integer i3 = new Integer( 3 ); Integer i4 = new Integer( 4 );
  Integer i5 = new Integer( 5 ); Integer i6 = new Integer( 6 );
  Integer i7 = new Integer( 7 ); Integer i8 = new Integer( 8 );
  Integer i9 = new Integer( 9 );

  IntegerDataBag bag = new IntegerDataBag();
  bag.add( i1 ); bag.add( i2 ); bag.add( i3 ); bag.add( i4 );
  bag.add( i5 ); bag.add( i6 ); bag.add( i7 ); bag.add( i8 );

  IntegerAdder adder = new IntegerAdder( bag );
  IntegerPrinter printer = new IntegerPrinter( bag );

  // adder and printer add themselves to the bag

  System.out.println( "About to add another integer to the bag:" );
  bag.add( i9 );
  System.out.println("");
  System.out.println("About to remove an integer from the bag:");
  bag.remove( 0 );
  }
}

運(yùn)行main,你將看到:

c:javaworldjava Driver
About to add another integer to the bag:
The contents of the IntegerDataBag have changed.
The new sum of the intergers is: 45
The contents of the IntegerDataBag have changed.
The new contents of the IntegerDataBag contains:
1
2
3
4
5
6
7
8
9

About to remove an integer from the bag:
The contents of the IntegerDataBag have changed.
The new sum of the intergers is: 44
The contents of the IntegerDataBag have changed.
The new contents of the IntegerDataBag contains:
2
3
4
5
6
7
8
9

IntegerDataBag/IntegerAdder/IntegerPrinter是應(yīng)用Observer模式的一個(gè)很簡(jiǎn)單的例子。Java本身有大量使用Observer模式的例子:AWT/swing事件模型,還有java.util.Observer和java.util.Observable接口等,都是很好的例子。

以上是“Java中如何使用Observer模式”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

網(wǎng)站題目:Java中如何使用Observer模式-創(chuàng)新互聯(lián)
文章起源:http://bm7419.com/article36/gogsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司定制網(wǎng)站、域名注冊(cè)、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司