java设计模式进阶_observer
来源:互联网 发布:数据库系统原理自考 编辑:程序博客网 时间:2024/06/03 15:06
////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : WeatherObserver.java// @ Date : 2016/8/31// @ Author : ////public interface WeatherObserver { public void update(WeatherType weatherType);}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : Hobbits.java// @ Date : 2016/8/31// @ Author : ////public class Hobbits implements WeatherObserver { public void update(WeatherType weatherType) { switch(weatherType) { case COLD: System.out.println("The hobbits are shivering in the cold weather."); break; case RAINY: System.out.println("The hobbits look for cover from the rain."); break; case SUNNY: System.out.println("The happy hobbits bade in the warm sun."); break; case WINDY: System.out.println("The hobbits hold their hats tightly in the windy weather."); break; default: break; } }}////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : Orcs.java// @ Date : 2016/8/31// @ Author : ////public class Orcs implements WeatherObserver { public void update(WeatherType weatherType) { switch(weatherType) { case COLD: System.out.println("The orcs are freezing cold."); break; case RAINY: System.out.println("The orcs are dripping wet."); break; case SUNNY: System.out.println("The sun hurts the orcs' eyes."); break; case WINDY: System.out.println("The orc smell almost vanishes in the wind."); break; default: break; } }}import java.util.ArrayList;import java.util.List;////// Generated by StarUML(tm) Java Add-In//// @ Project : Untitled// @ File Name : Weather.java// @ Date : 2016/8/31// @ Author : ////public class Weather { private WeatherType currentWeather; private List<WeatherObserver> observers; public Weather() { observers = new ArrayList<>(); currentWeather = WeatherType.SUNNY; } public void addObserver(WeatherObserver observer) { observers.add(observer); } public void removeObserver(WeatherObserver observer) { observers.remove(observer); } public void timePasses() { WeatherType[] enumValues = WeatherType.values(); currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length]; System.out.println("The weather changed to " + currentWeather + "."); notifyObservers(); } private void notifyObservers() { for(WeatherObserver obs : observers) { obs.update(currentWeather); } }}public enum WeatherType { SUNNY,//阳光的 RAINY,//下雨的 WINDY,//多风的 COLD;//寒冷的 public String toString() { return this.name().toLowerCase(); };}public class App { public static void main(String[] args) { Weather weather = new Weather(); weather.addObserver(new Orcs()); weather.addObserver(new Hobbits()); weather.timePasses(); weather.timePasses(); weather.timePasses(); weather.timePasses(); }}/*The weather changed to rainy.The orcs are dripping wet.The hobbits look for cover from the rain.The weather changed to windy.The orc smell almost vanishes in the wind.The hobbits hold their hats tightly in the windy weather.The weather changed to cold.The orcs are freezing cold.The hobbits are shivering in the cold weather.The weather changed to sunny.The sun hurts the orcs' eyes.The happy hobbits bade in the warm sun.*///示例2:public interface Observer<S extends Observable<S,O,A>, O extends Observer<S,O,A>, A> { void update(S subject,A argument);}import java.util.List;import java.util.concurrent.CopyOnWriteArrayList;public abstract class Observable<S extends Observable<S,O,A>,O extends Observer<S,O,A>,A> { protected List<O> observers; public Observable(){ this.observers = new CopyOnWriteArrayList<>(); } public void addObserver(O observer){ this.observers.add(observer); } @SuppressWarnings("unchecked") public void notifyObservers(A argument) { for(O observer : observers) { observer.update((S)this, argument); } }}import com.think.in.java.design.pattern.lesson22.WeatherType;public interface Race extends Observer<GWeather,Race,WeatherType>{}import com.think.in.java.design.pattern.lesson22.WeatherType;public class GHobbits implements Race{ @Override public void update(GWeather o, WeatherType weatherType) { switch (weatherType) { case COLD: System.out.println("The hobbits are shivering in the cold weather."); break; case RAINY: System.out.println("The hobbits look for cover from the rain."); break; case SUNNY: System.out.println("The happy hobbits bade in the warm sun."); break; case WINDY: System.out.println("The hobbits hold their hats tightly in the windy weather."); break; default: break; } }}import com.think.in.java.design.pattern.lesson22.WeatherType;public class GOrcs implements Race{ @Override public void update(GWeather o, WeatherType weatherType) { switch (weatherType) { case COLD: System.out.println("The orcs are freezing cold."); break; case RAINY: System.out.println("The orcs are dripping wet."); break; case SUNNY: System.out.println("The sun hurts the orcs' eyes."); break; case WINDY: System.out.println("The orc smell almost vanishes in the wind."); break; default: break; } }}import com.think.in.java.design.pattern.lesson22.WeatherType;public class GWeather extends Observable<GWeather,Race,WeatherType >{ private WeatherType currentWeather; public GWeather(){ currentWeather = WeatherType.SUNNY; } public void timePasses() { WeatherType[] enumValues = WeatherType.values(); currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length]; System.out.println("The weather changed to " + currentWeather + "."); notifyObservers(currentWeather); }}import com.think.in.java.design.pattern.lesson22.b.GHobbits;import com.think.in.java.design.pattern.lesson22.b.GOrcs;import com.think.in.java.design.pattern.lesson22.b.GWeather;public class App { public static void main(String[] args) { GWeather gWeather = new GWeather(); gWeather.addObserver(new GOrcs()); gWeather.addObserver(new GHobbits()); gWeather.timePasses(); gWeather.timePasses(); gWeather.timePasses(); gWeather.timePasses(); }}/*The weather changed to rainy.The orcs are dripping wet.The hobbits look for cover from the rain.The weather changed to windy.The orc smell almost vanishes in the wind.The hobbits hold their hats tightly in the windy weather.The weather changed to cold.The orcs are freezing cold.The hobbits are shivering in the cold weather.The weather changed to sunny.The sun hurts the orcs' eyes.The happy hobbits bade in the warm sun.*/
0 0
- java设计模式进阶_observer
- 学习设计模式(四)_observer
- 设计模式——观察者模式_Observer Pattern
- Java进阶-设计模式
- java设计模式进阶_singleton
- java设计模式进阶_prototype
- java设计模式进阶_builder
- java设计模式进阶_decorator
- java设计模式进阶_strategy
- java设计模式进阶_visitor
- java设计模式进阶_flyweight
- java设计模式进阶_proxy
- java设计模式进阶_facade
- java设计模式进阶_composite
- java设计模式进阶_bridge
- java设计模式进阶_adapter
- java设计模式进阶_interpreter
- java设计模式进阶_mediator
- java学习1
- 程序员笔试题收集汇总(一)
- InnoDB与Myisam的六大区别
- 5-53 两个有序序列的中位数 (25分)
- jq checkbox全选
- java设计模式进阶_observer
- Android 自定义屏幕中间Toast的不延时弹出
- 【Android 】TextView 局部文字变色
- 顶点Blinn-Phong.Shader
- AsyncQueryHandle
- MVC设计模式总结
- 使用MDK5建立STM32F407IG工程中遇到的问题
- maven项目java.lang.NoSuchMethodError,java.lang.NoClassDefFoundError
- Executors 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor