观察者模式
2015-11-15 17:25
225 查看
首先有一个更新天气布告板的系统。
public class ConditionsPanel{
public void update(float temp, float humidity, float pressure){
Debug.Log ("ConditionsPanel");
Debug.Log ("temp: " + temp);
Debug.Log ("humidity: " + temp);
Debug.Log ("pressure: " + temp);
}
}
public class StatisticsPanel{
public void update(float temp, float humidity, float pressure){
Debug.Log ("StatisticsPanel");
Debug.Log ("temp: " + temp);
Debug.Log ("humidity: " + temp);
Debug.Log ("pressure: " + temp);
}
}
public class ForecastPanel{
public void update(float temp, float humidity, float pressure){
Debug.Log ("ForecastPanel");
Debug.Log ("temp: " + temp);
Debug.Log ("humidity: " + temp);
Debug.Log ("pressure: " + temp);
}
}
public class WeatherData{
public ConditionsPanel conditionsDisplay = new ConditionsPanel();
public StatisticsPanel statisticsDisplay = new StatisticsPanel();
public ForecastPanel forecastPanel = new ForecastPanel();
public float getTemperature(){
return 1.0f;
}
public float getHumidity(){
return 2.0f;
}
public float getPressure(){
return 3.0f;
}
public void mentsChanged(){
float temp = getTemperature ();
float humidity = getHumidity ();
float pressure = getPressure ();
conditionsDisplay.update (temp, humidity, pressure);
statisticsDisplay.update (temp, humidity, pressure);
forecastPanel.update (temp, humidity, pressure);
}
}
注意到气象站提供getter方法供各种布告板使用,这里有3个布告板。
也就是一对多的关系。
更新布告板时,是针对具体实现编程,这将导致以后增加或者删除布告板时,需要修改或者代码。
当我们需要增加公告板时,除了要添加新的公告板类外,还需要修改WeatherDate的mentsChanged方法,公告板越来越多时,这段代码也会越来越冗长,稍有变动便要修改或删除。(两者耦合过紧,一方改动牵动着另一方)
公告板接收的参数是一样的,可以考虑整合。
//主题
public interface Subject{
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObserver();
}
//气象站
public class WeatherData : 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 notifyObserver(){
foreach (Observer o in observers) {
o.update(temperature, humidity, pressure);
}
}
public void measurementsChanged(){
notifyObserver ();
}
}
//观察者
public interface Observer{
void update(float temp, float humidity, float pressure);
}
//布告板
public class ConditionsPanel : Observer{
private float temperature;
private Subject weatherData;
//构造函数保存subject,并且注册自己
public ConditionsPanel(Subject weatherData){
this.weatherData = weatherData;
weatherData.registerObserver (this);
}
//观察者接口规定每个布告板都必须实现的统一接口
public void update(float temperature, float humidity, float pressure){
this.temperature = temperature;
4000
}
}
设计原则:
1.低耦合
为了交互对象之间的松耦合设计而努力。 针对接口编程让对象之间的依赖降低,达到松耦合的目的。
2.封装变化
3.针对接口编程,不针对实现编程
4.多用组合,少用继承
public class ConditionsPanel{
public void update(float temp, float humidity, float pressure){
Debug.Log ("ConditionsPanel");
Debug.Log ("temp: " + temp);
Debug.Log ("humidity: " + temp);
Debug.Log ("pressure: " + temp);
}
}
public class StatisticsPanel{
public void update(float temp, float humidity, float pressure){
Debug.Log ("StatisticsPanel");
Debug.Log ("temp: " + temp);
Debug.Log ("humidity: " + temp);
Debug.Log ("pressure: " + temp);
}
}
public class ForecastPanel{
public void update(float temp, float humidity, float pressure){
Debug.Log ("ForecastPanel");
Debug.Log ("temp: " + temp);
Debug.Log ("humidity: " + temp);
Debug.Log ("pressure: " + temp);
}
}
public class WeatherData{
public ConditionsPanel conditionsDisplay = new ConditionsPanel();
public StatisticsPanel statisticsDisplay = new StatisticsPanel();
public ForecastPanel forecastPanel = new ForecastPanel();
public float getTemperature(){
return 1.0f;
}
public float getHumidity(){
return 2.0f;
}
public float getPressure(){
return 3.0f;
}
public void mentsChanged(){
float temp = getTemperature ();
float humidity = getHumidity ();
float pressure = getPressure ();
conditionsDisplay.update (temp, humidity, pressure);
statisticsDisplay.update (temp, humidity, pressure);
forecastPanel.update (temp, humidity, pressure);
}
}
注意到气象站提供getter方法供各种布告板使用,这里有3个布告板。
也就是一对多的关系。
更新布告板时,是针对具体实现编程,这将导致以后增加或者删除布告板时,需要修改或者代码。
当我们需要增加公告板时,除了要添加新的公告板类外,还需要修改WeatherDate的mentsChanged方法,公告板越来越多时,这段代码也会越来越冗长,稍有变动便要修改或删除。(两者耦合过紧,一方改动牵动着另一方)
公告板接收的参数是一样的,可以考虑整合。
//主题
public interface Subject{
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObserver();
}
//气象站
public class WeatherData : 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 notifyObserver(){
foreach (Observer o in observers) {
o.update(temperature, humidity, pressure);
}
}
public void measurementsChanged(){
notifyObserver ();
}
}
//观察者
public interface Observer{
void update(float temp, float humidity, float pressure);
}
//布告板
public class ConditionsPanel : Observer{
private float temperature;
private Subject weatherData;
//构造函数保存subject,并且注册自己
public ConditionsPanel(Subject weatherData){
this.weatherData = weatherData;
weatherData.registerObserver (this);
}
//观察者接口规定每个布告板都必须实现的统一接口
public void update(float temperature, float humidity, float pressure){
this.temperature = temperature;
4000
}
}
设计原则:
1.低耦合
为了交互对象之间的松耦合设计而努力。 针对接口编程让对象之间的依赖降低,达到松耦合的目的。
2.封装变化
3.针对接口编程,不针对实现编程
4.多用组合,少用继承
相关文章推荐
- Java 打印三角形
- GDAL C# “OSGeo.GDAL.GdalPINVOKE”的类型初始值设定项引发异常 解决方法
- SparkSQL简单测试
- python项目内import其他内部package的模块的正确方法
- win10、win7系统64位oracle11g安装教程以及32位plsql连接教程
- 第一章-CSS和文档 ---元素
- Linux下C++访问MySQL数据库
- 自定义控件(19)---自定义控件之高仿猫眼小项目(1)
- 自己写的demo---声明异常同时处理异常,或者继续抛出异常
- 对象关系映射ORM?
- 第九周--数据结构--广义表算法库及应用
- HDOJ 4325 Flowers 【线段树 离散化 区间更新 单点查询】
- java 线程中断
- Scala基础入门-4
- 虚拟机检测技术剖析
- 基于AFNetworking封装网络库
- IE 兼容 getElementsByClassName
- http缓存
- 05 ueditor上传图片配置
- 几种排序算法