每个人都会死去,但不是每个人都曾经真正活过
Ever man dies,Not every man really lives
posts - 7,comments - 0,trackbacks - 0

   OBSERVER

      The Observer Pattern defines a one-to-many dependency between object so that when one object changes state ,all
of its dependents are notified and update automatically.
      观察者模式在对象间定义了一个一对多的关系,当一个对象(Subject 主题对象)发生改变时,所有依赖于该对象的对象都会被通知并且自动更新。
      
      The Observer Pattern provides an object design where subject and observer are loosely coupled:
      
         1> The only thing the subject knows about an observer is that it implement a certain interface.
         2> We can add new observer at any time.
         3> We never need to modify the subject to add new type of observers.
         4> Changes to either the subject or an observer will not affect other.
以下是测试代码
ProductObservable
/**
 * 
@author Wangzg
 * Time: 2006-9-12
 * FileName: ProductObservable.java
 * PackageName: src.observerPattern    
 * Description: Observable class  
 
*/

package src.observerPattern;

import java.util.Observable;

public class ProductObservable extends Observable {
    
private String name;

    
private double weigh;

    
public String getName() {
        
return name;
    }


    
public void setName(String name) {
        
this.name = name;
        setChanged();
        notifyObservers(name);
    }


    
public double getWeigh() {
        
return weigh;
    }


    
public void setWeigh(double weigh) {
        
this.weigh = weigh;
        setChanged();
        notifyObservers(
new Double(weigh));
    }

    
    
}


ProductNameobserver

  
/**
  * 
@author Wangzg
  * Time: 2006-9-12
  * FileName: ProductNameobserver.java
  * PackageName: src.observerPattern    
  * Description: Observer class 
  
*/

  
package src.observerPattern;

import java.util.Observable;
import java.util.Observer;

public class ProductNameobserver implements Observer {
    
private String name = null ;
    
/*
     * @author Wangzg
     * Time: 2006-9-12
     * MethodsName: 
     * variable in  :
     * variable out :
     * Description: 
     
*/

    
public void update(Observable o, Object arg) {
        
// TODO Auto-generated method stub
        if ( arg instanceof String){
            name 
= (String)arg;
            System.out.println(
"name change to "+name);
        }

        System.out.println(
"ProductNameobserver nothing happen");
    }


}



ProductWeighObserver
      
/**
 * 
@author Wangzg
 * Time: 2006-9-12
 * FileName: ProductWeighObserver.java
 * PackageName: src.observerPattern    
 * Description: Observer class 
 
*/

package src.observerPattern;

import java.util.Observable;
import java.util.Observer;

public class ProductWeighObserver implements Observer {
    
private douDoubleble weigh = 0.0;

    
/*
     * @author Wangzg Time: 2006-9-12 MethodsName: variable in : variable out :
     * Description:
     
*/

    
public void update(Observable o, Object arg) {
        
// TODO Auto-generated method stub
        if ( arg instanceof ProductObservable ){
            weigh 
= ((Double)arg).doubleValue();
            System.out.println(
"weigh change to "+weigh);
        }

        System.out.println(
"ProductWeighObserver nothing happen");
    }


}


posted on 2006-09-12 19:56 saftyfirst 阅读(149) 评论(0)  编辑  收藏 所属分类: 设计模式