完善观察者抽象.

pull/84/head
chen.ma 3 years ago
parent a2e98b0dc6
commit 5ba89d970a

@ -29,6 +29,16 @@ public class AbstractSubjectCenter {
register(SubjectType.SPRING_CONTENT_REFRESHED.name(), observer); register(SubjectType.SPRING_CONTENT_REFRESHED.name(), observer);
} }
/**
* Register observer.
*
* @param subjectType
* @param observer
*/
public static void register(SubjectType subjectType, Observer observer) {
register(subjectType.name(), observer);
}
/** /**
* Register observer. * Register observer.
* *
@ -75,6 +85,22 @@ public class AbstractSubjectCenter {
observers.remove(observer); observers.remove(observer);
} }
/**
* Notify.
*
* @param subjectType
* @param observerMessage
*/
public static void notify(SubjectType subjectType, ObserverMessage observerMessage) {
notify(subjectType.name(), observerMessage);
}
/**
* Notify.
*
* @param subject
* @param observerMessage
*/
public static void notify(String subject, ObserverMessage observerMessage) { public static void notify(String subject, ObserverMessage observerMessage) {
List<Observer> observers = OBSERVERS_MAP.get(subject); List<Observer> observers = OBSERVERS_MAP.get(subject);
if (CollectionUtil.isEmpty(observers)) { if (CollectionUtil.isEmpty(observers)) {
@ -91,12 +117,17 @@ public class AbstractSubjectCenter {
}); });
} }
private enum SubjectType { public enum SubjectType {
/** /**
* Spring content refreshed. * Spring content refreshed.
*/ */
SPRING_CONTENT_REFRESHED SPRING_CONTENT_REFRESHED,
/**
* Clear config cache.
*/
CLEAR_CONFIG_CACHE
} }

@ -6,13 +6,13 @@ package cn.hippo4j.common.design.observer;
* @author chen.ma * @author chen.ma
* @date 2021/12/25 19:46 * @date 2021/12/25 19:46
*/ */
public interface Observer { public interface Observer<T> {
/** /**
* Receive notification. * Receive notification.
* *
* @param observerMessage * @param observerMessage
*/ */
void accept(ObserverMessage observerMessage); void accept(ObserverMessage<T> observerMessage);
} }

@ -6,6 +6,13 @@ package cn.hippo4j.common.design.observer;
* @author chen.ma * @author chen.ma
* @date 2021/12/25 19:54 * @date 2021/12/25 19:54
*/ */
public interface ObserverMessage { public interface ObserverMessage<T> {
/**
* Message.
*
* @return
*/
T message();
} }

Loading…
Cancel
Save