设计模式(观察者模式)

观察者模式=发布者+订阅者

Subject.java

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


Observer.java

public interface Observer{
   public void update(float temp,float humidity,float pressure);
}


DisplayElement.java

public interface DisplayElement{
    public void display();
}

WeatherData.java
import java.util.ArrayList;
public class WeatherData implements Subject{
     private ArrayList observers;
	 private float temperature;
	 private float humidity;
	 private float pressure;
	 
	 public WeatherData(){
	     observers = new ArrayList();
	 }
	 
	 public void registerObserver(Observer o){
	     observers.add(o);
	}
	
	public void removeObserver(Observer o){
	   int i=observers.indexOf(o);
	   if(i>=0){
	       observers.remove(i);
	   }
    }
	
	public void notifyObservers(){
	    for(int i=0;i<observers.size();i++){
		    Observer observer=(Observer)observers.get(i);
			observer.update(temperature,humidity,pressure);
			
		}
	}
	
	public void measurementsChanged(){
	    notifyObservers();
	     
	} 
	
	
	
	public void setMeasurements(float temperature,float humidity,float pressure){
	     this.temperature = temperature;
		 this.humidity = humidity;
		 this.pressure = pressure;
		 measurementsChanged();
	}
}


CurrentConditionDisplay.java

import java.io.*;
public class CurrentConditionsDisplay implements Observer ,DisplayElement{
    private float temperature;
	private float humidity;
	private WeatherData weatherData;
	
	public CurrentConditionsDisplay(WeatherData weatherData_){
	      this.weatherData= weatherData_;
		  weatherData.registerObserver(this);
	}
	
	public void update(float temperature,float humidity,float pressure){
	    this.temperature = temperature;
		this.humidity = humidity;
		display();
	}
	
	
	
	public void display(){
	     System.out.println("Current condition :"+temperature+" F degrees and "+ humidity + "%humidity");
	}
}


WeatherStation.java

public class WeatherStation{
     public static void main(String[] args){
	     WeatherData weatherData = new WeatherData();
		 CurrentConditionsDisplay currentDisplay= new CurrentConditionsDisplay(weatherData);
		 
		 weatherData.setMeasurements(80,65,30.4f);

	}
}


这种编码方式不是最好的解决方法,有其他可以改进的N种方法。比如说采用MVC混合模式来设计。而且上面设计的代码 有个弊端就是,当客户端的一个发布站点要更新数据时,服务器端就需要遍历 更新  全部站点的数据并发布,其实只有客户端的数据一个数据变化了,其他的

仍然是原先的状态(值未变),既然值未变,服务端仍用原先值来做下更新的动作,是不是显得多此一举了呢。


下面是改进的一个方法:


Subject.java

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


Observer.java

public interface Observer{
    public void _setM(float temp,float humidity,float pressure);
}

DisplayElement.java

public interface DisplayElement{
    public void display();
}

WeatherData.java

import java.util.ArrayList;
public class WeatherData implements Subject{
     private ArrayList observers;
	 private float temperature;
	 private float humidity;
	 private float pressure;
	 
	 public WeatherData(){
	     observers = new ArrayList();
	 }
	 
	 public void registerObserver(Observer o){
	     observers.add(o);
	}
	
	public void removeObserver(Observer o){
	   int i=observers.indexOf(o);
	   if(i>=0){
	       observers.remove(i);
	   }
        }
	
	
	
	public void display(){
	    System.out.println("Current condition :"+temperature+" F degrees and "+ humidity + "%humidity");
	}
	
	public void setMeasurements(float temperature,float humidity,float pressure){
	     this.temperature = temperature;
	     this.humidity = humidity;
	     this.pressure = pressure;
	}
}

CurrentConditionDisplay.java

import java.io.*;
public class CurrentConditionsDisplay implements Observer ,DisplayElement{
    private float temperature;
	private float humidity;
	private WeatherData weatherData;
	
	public CurrentConditionsDisplay(WeatherData weatherData_){
	      this.weatherData= weatherData_;
		  weatherData.registerObserver(this);
	}
	
	
	
	public void _setM(float temperature,float humidity,float pressure){
	   weatherData.setMeasurements(temperature,humidity,pressure);
	}
	
	public void display(){
	    weatherData.display();
	}

}


WeatherStation.java
public class WeatherStation{
     public static void main(String[] args){
	     WeatherData weatherData = new WeatherData();
		 CurrentConditionsDisplay currentDisplay= new CurrentConditionsDisplay(weatherData);
		 
		
		 currentDisplay._setM(80,65,30.4f);
		 currentDisplay.display();
	}
}

当然,以上阐述有个bug,因为观察者模式就是默认通知所有“订阅者”的。根据需要来设计了。


内部类一例:

public class SwingObserverExample {
    JFrame frame;
    public static void main(String[] args) {
        SwingObserverExample example = new SwingObserverExample();
        example.go();
    }
    public void go() {
       frame = new JFrame();
       JButton button = new JButton("Should I do it?");
       button.addActionListener(new AngelListener());
       button.addActionListener(new DevilListener());
       frame.getContentPane().add(BorderLayout.CENTER, button);
       // 在这里设置frame属性
    }
    class AngelListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
          System.out.println("Don't do it, you might regret it!");
        }
    }
    class DevilListener implements ActionListener {
       public void actionPerformed(ActionEvent event) {
          System.out.println("Come on, do it!");
       }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值