Browse Source

观察者模式

code_hero 2 weeks ago
parent
commit
47d9558832

+ 15 - 0
src/main/java/observerPattern/main.java

@@ -0,0 +1,15 @@
+package observerPattern;
+
+import observerPattern.observer.impl.User;
+import observerPattern.subject.impl.WeChatPublicAccount;
+
+public class main {
+    public static void main(String[] args) {
+        WeChatPublicAccount weChatPublicAccount = new WeChatPublicAccount();
+        User zky = new User("zky", 18);
+        User abc = new User("abc", 19);
+        weChatPublicAccount.addObserver(abc);
+        weChatPublicAccount.addObserver(zky);
+        weChatPublicAccount.publish("你们好");
+    }
+}

+ 6 - 0
src/main/java/observerPattern/observer/Observer.java

@@ -0,0 +1,6 @@
+package observerPattern.observer;
+//观察者接口
+public interface Observer {
+    //更新
+    void update(String message);
+}

+ 37 - 0
src/main/java/observerPattern/observer/impl/User.java

@@ -0,0 +1,37 @@
+package observerPattern.observer.impl;
+
+import observerPattern.observer.Observer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class User implements Observer {
+    private static final Logger log = LoggerFactory.getLogger(User.class);
+    String name;
+    Integer age;
+
+    public User(String name, Integer age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Integer getAge() {
+        return age;
+    }
+
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+
+    @Override
+    public void update(String message) {
+        log.info("{}收到了消息: {}", name, message);
+    }
+}

+ 17 - 0
src/main/java/observerPattern/subject/Subject.java

@@ -0,0 +1,17 @@
+package observerPattern.subject;
+
+import observerPattern.observer.impl.User;
+
+//被观察者
+public interface Subject {
+    //添加观察者
+
+    void addObserver(User user);
+
+    //去除观察者
+
+    void removeObserver(User user );
+
+    //通知观察者
+    void notifyObservers(String message);
+}

+ 34 - 0
src/main/java/observerPattern/subject/impl/WeChatPublicAccount.java

@@ -0,0 +1,34 @@
+package observerPattern.subject.impl;
+
+import observerPattern.observer.impl.User;
+import observerPattern.subject.Subject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+import java.util.ArrayList;
+import java.util.List;
+//被观察者
+public class WeChatPublicAccount implements Subject {
+    private static final Logger log = LoggerFactory.getLogger(WeChatPublicAccount.class);
+    List<User> observers = new ArrayList<>();
+    @Override
+    public void addObserver(User user) {
+        observers.add(user);
+    }
+    @Override
+    public void removeObserver(User user ) {
+        observers.remove(user);
+    }
+
+    @Override
+    public void notifyObservers(String message) {
+        for (User observer : observers) {
+            observer.update(message);
+        }
+    }
+    public void publish(String message) {
+        log.info("微信发布消息");
+        notifyObservers(message);
+    }
+}