观察者设计模式

1.定义一个抽象Observe观察者

1 public interface Observe {
2     void update(Message message);
3 }

2.定义一个DBObserver观察者

public class DBObserver implements Observe {
    ObservedSubject observedSubject;
    String name;
    public DBObserver(String name,ObservedSubject observedSubject){
        this.name=name;
        this.observedSubject=observedSubject;
        observedSubject.addObserver(this);
    }
    @Override
    public void update(Message message) {
        System.out.println("数据库"+message.id+message.name+message.content);
    }
}

3.定义一个EmailObserver观察者

 1 public class EmailObserver implements Observe {
 2     ObservedSubject observedSubject;
 3     String name;
 4     public EmailObserver(String name,ObservedSubject observedSubject){
 5         this.name=name;
 6         this.observedSubject=observedSubject;
 7         observedSubject.addObserver(this);
 8     }
 9     @Override
10     public void update(Message message) {
11         System.out.println("邮件"+message.id+message.name+message.content);
12     }
13 }

4.定义一个AdminObserver观察者

 1 public class AdminObserver implements Observe {
 2     ObservedSubject observedSubject;
 3     String name;
 4     public AdminObserver(String name,ObservedSubject observedSubject){
 5         this.name=name;
 6         this.observedSubject=observedSubject;
 7         observedSubject.addObserver(this);
 8     }
 9     @Override
10     public void update(Message message) {
11         System.out.println("管理员!"+message.id+message.name+message.content);
12     }
13 }

5.定义一个ObservedSubject被观察者

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 public class ObservedSubject {
 5     List observers;
 6     private Message message;
 7     public ObservedSubject(){
 8         observers=new ArrayList<>();
 9     }
10     public void addObserver(Observe observe){
11         observers.add(observe);
12     }
13     public void removeObserver(Observe observe){
14         observers.remove(observe);
15     }
16     public void notifyObserver(){
17         for (Observe observer : observers) {
18             observer.update(message);
19         }
20     }
21     public void setMessage(Message message){
22         this.message=message;
23         this.notifyObserver();
24     }
25 }

6.消息Message类

 1 public class Message {
 2     int id;
 3     String name;
 4     String email;
 5     String content;
 6 
 7     public int getId() {
 8         return id;
 9     }
10 
11     public void setId(int id) {
12         this.id = id;
13     }
14 
15     public String getName() {
16         return name;
17     }
18 
19     public void setName(String name) {
20         this.name = name;
21     }
22 
23     public String getEmail() {
24         return email;
25     }
26 
27     public void setEmail(String email) {
28         this.email = email;
29     }
30 
31     public String getContent() {
32         return content;
33     }
34 
35     public void setContent(String content) {
36         this.content = content;
37     }
38 
39     public Message(int id, String name, String email, String content) {
40         this.id = id;
41         this.name = name;
42         this.email = email;
43         this.content = content;
44     }
45 }

7.测试

public class Main {

    public static void main(String[] args) {
        ObservedSubject observedSubject = new ObservedSubject();
        DBObserver mysql = new DBObserver("mysql",observedSubject);
        EmailObserver qq = new EmailObserver("qq",observedSubject);
        AdminObserver usr = new AdminObserver("usr", observedSubject);
        Message message = new Message(1, "hong", "[email protected]", "加油");
        observedSubject.setMessage(message);
    }
}

8.运行结果

观察者设计模式_第1张图片

 

你可能感兴趣的:(观察者设计模式)