views:

217

answers:

4

I have this class:

public abstract class AbstractIncomingCall {

    /*
    class properties
    */

    public void changeStatus(/*some parameters*/){
        //store parameters in class properties
        isValid();
    }

    protected abstract boolean isValid();
}

...which is extended by this class:

public class IncomingCallImpl extends AbstractIncomingCall{

    //I override the parent's method
    public void changeStatus(/*same parameters as parent's method*/) {
        super.changeStatus(/*same parameters as parent's method*/);
        //do something interesting 
    }

    protected boolean isValid() throws StatusChangeNotOccurredException {
        //implement my validation algorithm
    }

What I would like to achieve is ensuring that whenever changeStatus(/*some parameters*/) gets called the isValid() method is called; note that the isValid() method is implemented only in the concrete class, also it uses class properties inheritated from the parent class. Is there a way I can follow to ensure that isValid() is called other than calling super? I dislike very much the fact that I have to pass parameters all around, I think I'm going totally in the wrong direction and there is a cleaner way to achieve this. What I would like to keep in my code is the "isValid() calling logic" in the abstract class, because every call needs to be validated and I can't rely on me remembering this in the future :P

Thanks in advance :]

+13  A: 

It sounds like you want changeStatus() to follow the Template Method pattern. In this pattern, you define changeStatus() in the abstract class (making it final if you don't trust people to extend properly), and have it call the required methods:

public final void changeStatus()
{
    doSomethingSubclassSpecific();
    isValid()
}


protected abstract doSomethingSubclassSpecific();
kdgregory
Almost posted the same... this is a very common pattern.
Varkhan
The method must always be final (not only if don't trust people to extend properly). Template Method pattern implies the method is final, it the only way to guarantee the sequence of calls...
pgras
"The only way to guarantee" sounds a lot like distrusting extenders :-) but yes, I'd agree with you that it's the proper implementation
kdgregory
Always distrust extenders - it's not that they are stupid, they are just not mindreaders.
DJClayworth
+2  A: 

You don't need to override changeStatus(). You are very close to implement the Template Method design pattern.

daanish.rumani
+2  A: 

You could make changeStatus final, and add to it the call to a customChangeStatus method that could be overridden. like below:

public abstract class AbstractIncomingCall {

/*
class properties
*/

public final void changeStatus(/*some parameters*/){
    //store parameters in class properties
    isValid();
    customChangeStatus();
}

protected abstract void customChangeStatus();
protected abstract boolean isValid();

}

You could even implement an empty method for customChangeStatus so you don't need to always implement it. Using the final in the changeStatus you are sure that will be the logic when calling your classes. And you still have the flexibility to add custom behavior to it.

Rafael Coutinho
A: 

I suggest using composition rather than inheritance. Things should become more obvious that way.

Tom Hawtin - tackline
Could you be more precise on what you mean?
Alberto Zaccagni