observer模式

博客展示了Java中使用观察者模式来捕捉对象变化的代码实现。定义了Product类作为被观察对象,包含名称和价格属性,同时创建了NameObserver和PriceObserver两个观察者类。在Main类中创建Product对象并添加观察者,通过修改对象属性触发观察者的更新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值