收藏本页 | 网站地图 | 投稿指南
 
 
当前位置:首页 >> 学院首页 >> 程序开发 >> JSP >>

observer模式

放大字体  缩小字体  At: 2005-10-04 02:15  By: master8 转载 来源: 互联网
给某个对象一个设置一个观察者,以便能敏感捕捉到对象的变化
package observer;

import java.util.*; 
/**
* 此处插入类型说明。
* 创建日期:(2002-9-4 17:17:05)
* @author:Administrator
*/
public class Product extends Observable {
 private String name;
 private float price;
 public String getName() {
  return name;
 }

 public float getPrice() {
  return price;
 }

 private int id;

 public Product(int id, String name, float price) {
  this.id = id;
  this.name = name;
  this.price = price;
 }
 public int getId() {
  return id;
 }
 public void setName(String name) {
  this.name = name;
  setChanged();
  notifyObservers("name");
 }
 public void setPrice(float price) {
  this.price = price;
  setChanged();
  notifyObservers("price");
  //notifyObservers();          
 }
}
****************************************************************
package observer;

import java.util.*;/**
 * 此处插入类型说明。
 * 创建日期:(2002-9-4 17:17:05)
 * @author:Administrator
 */
public class PriceObserver implements Observer {
 private float price = 0;
 public void update(Observable obj, Object arg) {

  try{   
   if (arg instanceof String) {
    String argName = (String)arg;
    if(argName.trim().equalsIgnoreCase("price")){
     int id = 0;    
     float price = 0;   
     String test = "";
     if(obj instanceof Product){
      Product pro = (Product)obj;
      price = pro.getPrice(); 
      id = pro.getId(); 
     }  
     
     System.out.println("PriceObserver : Product "+id+" "s pirce has changed to " + price);
    }
   }   
   
  }catch(Exception ex){
   ex.printStackTrace();
  }  
  
 }
}
*********************************************************************
package observer;

import java.util.*;
public class NameObserver implements Observer {
 private String name = null;
 public void update(Observable obj, Object arg) {
  try{
   
   if (arg instanceof String) {
    String argName = (String)arg;
    if(argName.trim().equalsIgnoreCase("name")){
     int id = 0;    
     String name = "";   
     if(obj instanceof Product){
      Product pro = (Product)obj;
      name = pro.getName();
      id = pro.getId(); 
     }      
     System.out.println("NameObserver : Product "+id+" "s name has changed to " + name);
    }
   }
   
   
  }catch(Exception ex){
   ex.printStackTrace();
  }
 }
}
*********************************************************************
package observer;

/**
 * 此处插入类型说明。
 * 创建日期:(2002-9-4 17:17:05)
 * @author:Administrator
 */
public class Main {
 public static void main(String args[]) {
  Product product = new Product(1,"桔子",(float)9.20);
  //Product product = new Product();
  NameObserver nameobs = new NameObserver();
  PriceObserver priceobs = new PriceObserver();
  product.addObserver(nameobs);
  product.addObserver(priceobs);
  product.setName("苹果");
  product.setPrice(12.80f);
  product.setName("香蕉");
  product.setPrice(6.00f);
 }
}
 






         









 
Google
论坛精华  
 
 
  ©2005-2008 站长吧 Master8.NET All Rights Reserved 陕ICP备05010609号