Question

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

Was it helpful?

Solution 2

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

Walter

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top