<h3>引入:</h3>
<h5>需求:</h5>有一个气象台每隔1~1.5秒会更新一次天气,那么学生应该在其每次更新天气的时候能够实时的接收到最新天气且做出响应
学生类不变
public class Student implements Weather{
private String name;
public Student(String name){
this.name = name;
}
@Override
public void observerWeather(String curWeather) {
//晴天","暴雨","霜冻","台风","冰雹","雾霾"};
switch(curWeather){
case "晴天":
System.out.println(this.name+":天气这么好,应该去走走");
break;
case "暴雨":
System.out.println(this.name+":这么大的雨就别去上课了");
break;
case "霜冻":
System.out.println(this.name+":好冷啊,还是在宿舍睡觉吧");
break;
case "台风":
System.out.println(this.name+":台风来啦,好可怕,还上课干嘛");
break;
case "冰雹":
System.out.println(this.name+":你想被砸死吗?反正我是不会去上课的");
break;
case "雾霾":
System.out.println(this.name+":麻烦你帮我跟老师请个假,我中毒不轻");
break;
}
}
}
实现一:
class WeatherStation{
String []weathers={"晴天","暴雨","霜冻","台风","冰雹","雾霾"};
String curWeather;
Random random = new Random();
public void startWork(){
new Thread(){
@Override
public void run() { //用一个逆名内部类来开启一个新线程
while(true){
updateWeather();
//每 隔1~1.5秒更新一次天气
int millis = random.nextInt(501)+1000;
try {
Thread.sleep(millis);//新线程睡眠
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
private void updateWeather(){
int sel = random.nextInt(6);
curWeather = weathers[sel];
System.out.println("气象台:今天天气"+curWeather);
}
}
class Demo01{
public static void main(String args[]){
WeatherStation ws = new WeatherStation ();
ws.startWork();
Student stu = new Student("丁昌江");
Random random = new Random();
while(true){
int millis = random.nextInt(501)+1000;
try {
Thread.sleep(millis);
stu.observerWeather(ws.curWeather);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
结果:并没有实现天气更新一次,学生就会作出一次反应,因为两个线程,两个随机的时间都可能造成偏差,导致不同步
气象台:今天天气雾霾
气象台:今天天气冰雹
丁昌江:你想被砸死吗?反正我是不会去上课的
气象台:今天天气霜冻
丁昌江:好冷啊,还是在宿舍睡觉吧
丁昌江:好冷啊,还是在宿舍睡觉吧
气象台:今天天气霜冻
实现二:
class WeatherStation{
String []weathers={"晴天","暴雨","霜冻","台风","冰雹","雾霾"};
String curWeather;
Random random = new Random();
Student stu;
public void add(Student stu){
this.stu = stu;
}
public void startWork(){
new Thread(){
@Override
public void run() {
while(true){
updateWeather();
stu.observerWeather(curWeather);
//每 隔1~1.5秒更新一次天气
int millis = random.nextInt(501)+1000;
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
private void updateWeather(){
int sel = random.nextInt(6);
curWeather = weathers[sel];
System.out.println("气象台:今天天气"+curWeather);
}
class Demo01{
public static void main(String args[]){
WeatherStation ws = new WeatherStation ();
Student stu = new Student("丁昌江");
ws.add(stu);
ws.startWork();
}
}
结果:似乎好像已经达到了目的,但是还有两个局限,一:只能通知一个学生对象????二:只能通知学生吗?其他群体咧,难道国家就只重视国学的小花朵吗?
气象台:今天天气霜冻
丁昌江:好冷啊,还是在宿舍睡觉吧
气象台:今天天气雾霾
丁昌江:麻烦你帮我跟老师请个假,我中毒不轻
气象台:今天天气晴天
丁昌江:天气这么好,应该去走走
气象台:今天天气暴雨
丁昌江:这么大的雨就别去上课了
<h3>观察者设计模式:</h3>
Weather接口:制定一个weather接口,只要实现了该接口的类都可以被气象体通知
interface Weather {
void observerWeather (String cuWeather);
}
WeatherStation类:里面维护一个list来存储当前订阅了气象台最新天气的用户,当有新天气时,会遍历该list去挨个通知用户
class WeatherStation{
String []weathers={"晴天","暴雨","霜冻","台风","冰雹","雾霾"};
String curWeather;
Random random = new Random();
List<Weather> list = new ArrayList<Weather>();
public void add(Weather w){
list.add(w);
}
public void startWork(){
new Thread(){
@Override
public void run() {
while(true){
updateWeather();
//每 隔1~1.5秒更新一次天气且遍历通知list中的对象
for(Weather e:list){
e.observerWeather(curWeather);
}
int millis = random.nextInt(501)+1000;
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
private void updateWeather(){
int sel = random.nextInt(6);
curWeather = weathers[sel];
System.out.println("气象台:今天天气"+curWeather);
}
}
Emp类:(同样实现了Weather接口)
public class Emp implements Weather{
private String name;
public Emp(String name){
this.name = name;
}
@Override
public void observerWeather(String curWeather) {
switch(curWeather){
case "晴天":
System.out.println(this.name+":天气这么好,肯定得上班呀");
break;
case "暴雨":
System.out.println(this.name+":下雨?已经不错了,上班去吧");
break;
case "霜冻":
System.out.println(this.name+":买个棉大衣,上班去吧");
break;
case "台风":
System.out.println(this.name+":台风来了,你以为就不用上班了吗?");
break;
case "冰雹":
System.out.println(this.name+":反正我是刚网购了一个头盔");
break;
case "雾霾":
System.out.println(this.name+":来来来,每个人分一个防毒面具,上班去");
break;
}
}
}
Main类:主类
class Main{
public static void main(String args[]){
WeatherStation ws = new WeatherStation ();
ws.add(new Student("丁昌江"));
ws.add(new Emp("吴云飞"));
ws.startWork();
}
}
结果:
气象台:今天天气冰雹
丁昌江:你想被砸死吗?反正我是不会去上课的
吴云飞:反正我是刚网购了一个头盔
气象台:今天天气霜冻
丁昌江:好冷啊,还是在宿舍睡觉吧
吴云飞:买个棉大衣,上班去吧
气象台:今天天气暴雨
丁昌江:这么大的雨就别去上课了
吴云飞:下雨?已经不错了,上班去吧