views:

26

answers:

2

Hi all,

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

A: 

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

Steve Emmerson
That's what I was doing, I updated my question to include a very rough outline of my code. It does exactly what you say. The problem with this is that the concrete implementation in the superclass is never invoked by the container (Glassfish 3.1 embedded).
What you describe shouldn't happen. Sounds like a problem with Glassfish.
Steve Emmerson
A: 

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

Walter