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
原创粉丝点击