|
@@ -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);
|
|
|
+ }
|
|
|
+}
|