开发者

CDI - Event Observers

I have event observers which all observe the same event, so I have an abstract superclass which observes that event and then the subclass overrides / implements the specific functionality.

The problem with this is that it doesn't observe the event unless I put the event observer method in the subclass (which defeats the purpose of my design).

I can most certainly do it this way, but I want to write as little code as possible.

Should it work this way (am I doing something else wrong)?

If it isn't supposed to work this way, then I can write a producer for one thing and then an interceptor. I thought that my first approach was simpler and more likely to be used properly by other developers.

example code:

SuperClass:

public abstract class AbstractFileWriter
{
  public void onReady(@Observes ReadyEvent readyEvent)
  {
    try
    {
      handleReady(readyEvent, ...);
    }
  }

  protected abstract handleReady(ReadyEvent readyEvent, otherParameters go here);
}

SubClass

public class XmlWriter extends AbstractFileWriter
{
   protected handleReady( ... )
   { ...开发者_如何转开发 }
}

If I write it this way, handleReady is never invoked (and neither is onReady for that matter); however, if I write it with the observer method within the subclass, it works as expected. I want to write it this way as there is much less code I'd have to write and a little bit less abstraction which should make it easier to understand.

Walter


Either declare the relevant method in the superclass to be abstract or have it call an abstract "detail" method that each subclass implements.


I wrote a decorator as I mentioned above. This essentially does the same thing, just written slightly differently.

Walter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜