java设计模式进阶_observer

原创 2016年08月31日 11:57:05

这里写图片描述

//
//
//  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.

*/


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Java常用类库--观察者设计模式( Observable类Observer接口)

如果要想实现观察者模式,则必须依靠java.util包中提供的Observable类和Observer接口。 [java] view plaincopy import jav...

JAVA设计模式-Observer

Java提供了Observer模式的默认实现,下面我们就看一下Java对观察者模式的支持。     Java对观察者模式的支持主要体现在Observable类和Observer接口。   (1...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

java设计模式进阶_abstract-factory

欢迎同行朋友访问我的博客。本人学习计算机已有几年时间了,一直在苦苦探索,其中有苦有乐。本人本着实事求是的态度学习java,有些微体会在此分享一下。从事这几年web开发曾经不止一次问自己假如领导把一个项...

Java进阶之Jsp设计模式知识点总结

Model1和Model2------设计模式是JSP技术发展的初始阶段,Model1模式被广泛的应用。

java设计模式进阶_double-checked-locking

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : Item.ja...

JavaSE进阶-Java设计模式

本笔记基于: [1]传智播客_java学科2014年2月份设计模式公开课 [2]传智播客Java设计模式年第二次公开课视频和资料公开课 [3] GOF设计模式_尚学堂官网 备用链接:链接: ht...

java设计模式进阶_visitor

// // // Generated by StarUML(tm) Java Add-In // // @ Project : Untitled // @ File Name : UnitVis...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)