使用 Spring 依赖注入实现观察者模式

发布于 2024-09-08 15:39:08 字数 1332 浏览 7 评论 0原文

我正在尝试开发一个应用程序,如果零件编号有任何变化,人们就会收到通知。它是一个基于 Spring 的应用程序。以下是代码片段。

我在 SaveSubscriptionIF 中抽象了存储机制。存储和检索的实际细节在该接口的实现类中。实现类将由Spring注入。

外部应用程序调用我的 setState() 方法 - 传递新信息。

store.getObservers() 方法返回 Map,其中包含观察者和要更新的相应信息。

到目前为止,一切都很好。我对 store.getObservers() 的实现部分感到震惊。此方法实现需要状态数据 - 即从外部应用程序传递的新信息。

如何将“状态”数据传递给“SaveSubscriptionIF”的实现类?

public class PartNumberSubject implements Observable {
     private SaveSubscriptionIF store;
     private Object state;
     @Override
     public void addObserver(final ObserverIF o, final Object subscriptionDetails) {
      store.addObserver(o, subscriptionDetails);
     }
     @Override
     public void notifyObservers() {
      @SuppressWarnings("unchecked")
      final Map<ObserverIF, List<PartInformationVO>> observers =(Map<ObserverIF,List<PartInformationVO>>) store                 .getObservers();
  for (final Entry<ObserverIF, List<PartInformationVO>> observer : observers
                        .entrySet()) {
     observer.getKey().update(observer.getValue());
     }
    }
    @Override
    public void setState(final Object state) {
     this.state = state;
     notifyObservers();
    }
    }

I'm trying to develop an application where in the people will get notified if there is any change in the PartNumber. Its a Spring based application. Following is the code snippet.

I've abstracted the storing mechanism here in SaveSubscriptionIF. The actual details of storing and retrieving are in the implementation class of this interface. The implementation class will be injected by Spring.

The external application calls my setState() method- passing the new information.

The store.getObservers() method returns me the Map which have the Observers and the corresponding info to update.

So far so good. Am struck in the implementation part of store.getObservers(). This method implementation needs the state data - i.e the new information passed from the external application.

How do i pass the 'state' data to an implementation class of 'SaveSubscriptionIF'??

public class PartNumberSubject implements Observable {
     private SaveSubscriptionIF store;
     private Object state;
     @Override
     public void addObserver(final ObserverIF o, final Object subscriptionDetails) {
      store.addObserver(o, subscriptionDetails);
     }
     @Override
     public void notifyObservers() {
      @SuppressWarnings("unchecked")
      final Map<ObserverIF, List<PartInformationVO>> observers =(Map<ObserverIF,List<PartInformationVO>>) store                 .getObservers();
  for (final Entry<ObserverIF, List<PartInformationVO>> observer : observers
                        .entrySet()) {
     observer.getKey().update(observer.getValue());
     }
    }
    @Override
    public void setState(final Object state) {
     this.state = state;
     notifyObservers();
    }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2024-09-15 15:39:08

我想我明白你的意思。

通常在观察者模式中,更改的值会作为创建的 Event 对象中的参数传递出去。

因此,例如,您可以创建一个 StateChangedEvent 类,其中包含一个值 - state。

然后,您的 setState 方法将如下所示:

public void setState(final Object state) {
    this.state = state;
    notifyObservers(state);
}

并且 notificationObservers:

public void notifyObservers(Object state) {
    StateChangedEvent event = new StateChangedEvent(state);
    // Pass this event into each observer which you have subscribed...
}

如果我误读了问题,我很抱歉,但这似乎就是您要问的。

I think I understand what you mean.

Normally in the observer pattern, the value that changed is passed out as a parameter in the Event object that is created.

So, for example, you could create a StateChangedEvent class which had a value in it - state.

Then, your setState method would look like this:

public void setState(final Object state) {
    this.state = state;
    notifyObservers(state);
}

And notifyObservers:

public void notifyObservers(Object state) {
    StateChangedEvent event = new StateChangedEvent(state);
    // Pass this event into each observer which you have subscribed...
}

I'm sorry if I've misread the question but this seems to be what you're asking.

荆棘i 2024-09-15 15:39:08

将数据传递给观察者是通过观察者的方法完成的。所以修改你的接口:

public ObserverIF {
    void update(List<PartInformationVO>, Object state);
}

Passing data to observers is done via the observer's methods. So modify your interface:

public ObserverIF {
    void update(List<PartInformationVO>, Object state);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文