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 😛
Thanks in advance :]
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: