网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

observer模式

http://www.diybl.com/ 2007-2-6  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】

给某个对象一个设置一个观察者,以便能敏感捕捉到对象的变化
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);
 }
}

如果图片或页面不能正常显示请点击这里 站内搜索:   
上一篇文章:Builder(建造)模式
下一篇文章:关于三层结构

文章评论

请您留言