The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Java: Superclasses and Subclasses

Using Java, I want to create a superclass containing some abstract methods, but in the implementing methods in the subclass, I want control to pass back to a superclass method without having to explicitly invoke the method in the subclass:

abstract class A {
  public abstract void doSomething();
  protected void cleanUp() {
    .
    .
  }
}

class B extends A {
  public void doSomething() {
    // Do stuff
    .
    .
    // Want to invoke super.cleanUp here without
    // having to do it explicitly
  }
}

Is there a pattern for doing this? It feels like a cross between the Template Method and Inversion of Control.
JP
Tuesday, April 10, 2007
 
 
One idea would be to encapsulate the doSomething in a wrapper method. For example:

public abstract class A {
    protected abstract void doSomething();
    protected void run() {
        try {
            doSomething();
        } catch( Exception e ) {
            System.err.println(e);
        } finally {
            cleanUp();
        }
    }
    protected void cleanUp() {
        System.err.println("Cleaned Up");
    }
}

public class B extends A {

    public void doSomething() {
        System.err.println("Entered doSomething");
    }

    public static void main(String[] args) {
        B b = new B();
        b.run();
    }

}
Just A Guy
Tuesday, April 10, 2007
 
 
I would modify Just A Guy's solution a little: The exposed method in the base class (run) would be public and final, and the cleanUp method would be protected and final.  The doSomething in B would be protected (not more visible than the base).
bmm6o Send private email
Tuesday, April 10, 2007
 
 
My question is "why don't you want to call cleanup() explicitly? If you want it to happen then calling it would seem to be a normal thing to do.

Maybe you should think a bit more about exactly when you want cleanup() to happen. Should it happen at the end of doSomething() every time it is called in class B? Or should it always happen at the end of doSomething()? Do you want to make sure that classes C and D (which also re-implement doSomething()) always call cleanup() too? Maybe C and D have to do their own cleanup as well as what A does.

The answers to these questions will probably decide how you should structure this.
DJ Clayworth
Tuesday, April 10, 2007
 
 
Just for completeness sake, the solution of Just A Guy is a pattern called template method : http://en.wikipedia.org/wiki/Template_method_pattern
Monkeyget Send private email
Wednesday, April 11, 2007
 
 
MonkeyGet:
I knew I'd seen it somewhere. I'm terrible with remembering the names of the patterns.
Just A Guy Send private email
Wednesday, April 11, 2007
 
 
Not that I necessarily recommend doing it this way, but check out JUnit 4 which effective uses annotations (@Before, @After) to do what you're asking.
dot Send private email
Thursday, April 12, 2007
 
 
> // Want to invoke super.cleanUp here without
> // having to do it explicitly

If the subclass does not have a cleanUp method, then just call the method and the call will be properly resolved.

Or do you mean, have cleanUp invoked without an explicit call to it?
dot for this one
Thursday, April 12, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz