简体   繁体   English

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

[英]Observer pattern implementation using Spring dependency injection

I'm trying to develop an application where in the people will get notified if there is any change in the PartNumber. 我正在尝试开发一个应用程序,如果PartNumber有任何更改,人们将收到通知。 Its a Spring based application. 它是一个基于Spring的应用程序。 Following is the code snippet. 以下是代码段。

I've abstracted the storing mechanism here in SaveSubscriptionIF . 我已经在SaveSubscriptionIF抽象了存储机制。 The actual details of storing and retrieving are in the implementation class of this interface. 存储和检索的实际详细信息在此接口的实现类中。 The implementation class will be injected by Spring. 实现类将由Spring注入。

The external application calls my setState() method- passing the new information. 外部应用程序调用我的setState()方法,传递新信息。

The store.getObservers() method returns me the Map which have the Observers and the corresponding info to update. store.getObservers()方法向我返回具有观察者和要更新的相应信息的Map

So far so good. 到现在为止还挺好。 Am struck in the implementation part of store.getObservers() . store.getObservers()的实现部分击中。 This method implementation needs the state data - ie the new information passed from the external application. 此方法实现需要state数据-即从外部应用程序传递的新信息。

How do i pass the 'state' data to an implementation class of 'SaveSubscriptionIF'?? 如何将“状态”数据传递给“ 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 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. 通常,在观察者模式中,更改后的值将作为所创建的Event对象中的参数传递出去。

So, for example, you could create a StateChangedEvent class which had a value in it - state. 因此,例如,您可以创建一个StateChangedEvent类,其中包含一个值StateChangedEvent

Then, your setState method would look like this: 然后,您的setState方法将如下所示:

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. 很抱歉,如果我没有正确阅读问题,但这似乎就是您要问的问题。

Passing data to observers is done via the observer's methods. 通过观察者的方法将数据传递给观察者。 So modify your interface: 因此,修改您的界面:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM