Skip to content

Commit cf12ff1

Browse files
committed
observer
1 parent fe7683b commit cf12ff1

File tree

7 files changed

+111
-90
lines changed

7 files changed

+111
-90
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.anxpp.designpattern.Observer;
2+
23
//观察者
34
public interface Client {
4-
void getWeather(WeatherInfo info);
5+
void getWeather(WeatherInfo info);
56
}
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.anxpp.designpattern.Observer;
22

33
public class ClientAndroidServer implements Client {
4-
private static String name = "安卓服务";
5-
private WeatherInfo info;
6-
@Override
7-
public void getWeather(WeatherInfo info) {
8-
this.info = info;
9-
dealMsg();
10-
}
11-
private void dealMsg(){
12-
System.out.println(name + "收到最新天气:time="+info.getTime()+"msg="+info.getWeather()+"。马上开始推送消息...");
13-
}
4+
private static String name = "安卓服务";
5+
private WeatherInfo info;
6+
7+
@Override
8+
public void getWeather(WeatherInfo info) {
9+
this.info = info;
10+
dealMsg();
11+
}
12+
13+
private void dealMsg() {
14+
System.out.println(name + "收到最新天气:time=" + info.getTime() + "msg=" + info.getWeather() + "。马上开始推送消息...");
15+
}
1416
}
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.anxpp.designpattern.Observer;
22

33
public class ClientIphoneServer implements Client {
4-
private static String name = "苹果服务";
5-
private WeatherInfo info;
6-
@Override
7-
public void getWeather(WeatherInfo info) {
8-
this.info = info;
9-
dealMsg();
10-
}
11-
private void dealMsg(){
12-
System.out.println(name + "收到最新天气:time="+info.getTime()+"msg="+info.getWeather()+"。马上开始推送消息...");
13-
}
4+
private static String name = "苹果服务";
5+
private WeatherInfo info;
6+
7+
@Override
8+
public void getWeather(WeatherInfo info) {
9+
this.info = info;
10+
dealMsg();
11+
}
12+
13+
private void dealMsg() {
14+
System.out.println(name + "收到最新天气:time=" + info.getTime() + "msg=" + info.getWeather() + "。马上开始推送消息...");
15+
}
1416
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.anxpp.designpattern.Observer;
2+
23
//主题
34
public interface IWeatherService {
4-
void addClient(Client client); //添加观察者
5-
boolean deleteClient(Client client);//删除观察者
6-
void notifyClients(); //通知
7-
void updateWeather(WeatherInfo info);//主题内容更新
5+
void addClient(Client client); //添加观察者
6+
7+
boolean deleteClient(Client client);//删除观察者
8+
9+
void notifyClients(); //通知
10+
11+
void updateWeather(WeatherInfo info);//主题内容更新
812
}

src/com/anxpp/designpattern/Observer/TestUse.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33

44
public class TestUse {
5-
public static void main(String args[]){
6-
//创建主题
7-
WeatherService service = WeatherService.instance;
8-
//添加观察者
9-
service.addClient(new ClientAndroidServer());
10-
service.addClient(new ClientIphoneServer());
11-
//更新主题
12-
service.updateWeather(new WeatherInfo(System.currentTimeMillis(), "多云"));
13-
service.updateWeather(new WeatherInfo(System.currentTimeMillis()+1000*60*60*24, "多云转晴"));
14-
service.updateWeather(new WeatherInfo(System.currentTimeMillis()+1000*60*60*24*2, "晴"));
15-
}
5+
public static void main(String args[]) {
6+
//创建主题
7+
WeatherService service = WeatherService.instance;
8+
//添加观察者
9+
service.addClient(new ClientAndroidServer());
10+
service.addClient(new ClientIphoneServer());
11+
//更新主题
12+
service.updateWeather(new WeatherInfo(System.currentTimeMillis(), "多云"));
13+
service.updateWeather(new WeatherInfo(System.currentTimeMillis() + 1000 * 60 * 60 * 24, "多云转晴"));
14+
service.updateWeather(new WeatherInfo(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 2, "晴"));
15+
}
1616
}
Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
package com.anxpp.designpattern.Observer;
2+
23
//天气的消息实体
34
public class WeatherInfo {
4-
private long time;
5-
private String weather;
6-
public WeatherInfo(long time,String weather){
7-
this.time = time;
8-
this.weather = weather;
9-
}
10-
public long getTime() {
11-
return time;
12-
}
13-
public void setTime(long time) {
14-
this.time = time;
15-
}
16-
public String getWeather() {
17-
return weather;
18-
}
19-
public void setWeather(String weather) {
20-
this.weather = weather;
21-
}
22-
@Override
23-
public boolean equals(Object obj) {
24-
WeatherInfo info = (WeatherInfo) obj;
25-
return info.time==this.time&&info.weather.equals(this.weather);
26-
}
5+
private long time;
6+
private String weather;
7+
8+
public WeatherInfo(long time, String weather) {
9+
this.time = time;
10+
this.weather = weather;
11+
}
12+
13+
public long getTime() {
14+
return time;
15+
}
16+
17+
public void setTime(long time) {
18+
this.time = time;
19+
}
20+
21+
public String getWeather() {
22+
return weather;
23+
}
24+
25+
public void setWeather(String weather) {
26+
this.weather = weather;
27+
}
28+
29+
@Override
30+
public boolean equals(Object obj) {
31+
WeatherInfo info = (WeatherInfo) obj;
32+
return info.time == this.time && info.weather.equals(this.weather);
33+
}
2734
}

src/com/anxpp/designpattern/Observer/WeatherService.java

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,41 @@
33
import java.util.Iterator;
44
import java.util.LinkedHashSet;
55
import java.util.LinkedList;
6+
67
//主题,同时使用了枚举实现单例
7-
public enum WeatherService implements IWeatherService{
8-
instance;
9-
private LinkedList<WeatherInfo> weatherInfos = new LinkedList<WeatherInfo>();
10-
private LinkedHashSet<Client> clients = new LinkedHashSet<Client>(); //存放观察者
11-
//添加观察者
12-
@Override
13-
public void addClient(Client client) {
14-
clients.add(client);
15-
}
16-
//删除观察者
17-
@Override
18-
public boolean deleteClient(Client client) {
19-
return clients.remove(client);
20-
}
21-
//通知观察者
22-
@Override
23-
public void notifyClients() {
24-
Iterator<Client> iterator = clients.iterator();
25-
while(iterator.hasNext()){
26-
iterator.next().getWeather(weatherInfos.peekFirst());
27-
}
28-
}
29-
//更新天气
30-
@Override
31-
public void updateWeather(WeatherInfo info) {
32-
if(weatherInfos.size()>0)
33-
if(weatherInfos.peekFirst().equals(info)) return;
34-
weatherInfos.push(info);
35-
if(clients.size()==0) return;
36-
notifyClients();
37-
}
8+
public enum WeatherService implements IWeatherService {
9+
instance;
10+
private LinkedList<WeatherInfo> weatherInfos = new LinkedList<WeatherInfo>();
11+
private LinkedHashSet<Client> clients = new LinkedHashSet<Client>(); //存放观察者
12+
13+
//添加观察者
14+
@Override
15+
public void addClient(Client client) {
16+
clients.add(client);
17+
}
18+
19+
//删除观察者
20+
@Override
21+
public boolean deleteClient(Client client) {
22+
return clients.remove(client);
23+
}
24+
25+
//通知观察者
26+
@Override
27+
public void notifyClients() {
28+
Iterator<Client> iterator = clients.iterator();
29+
while (iterator.hasNext()) {
30+
iterator.next().getWeather(weatherInfos.peekFirst());
31+
}
32+
}
33+
34+
//更新天气
35+
@Override
36+
public void updateWeather(WeatherInfo info) {
37+
if (weatherInfos.size() > 0)
38+
if (weatherInfos.peekFirst().equals(info)) return;
39+
weatherInfos.push(info);
40+
if (clients.size() == 0) return;
41+
notifyClients();
42+
}
3843
}

0 commit comments

Comments
 (0)