-5

I've a class which performs an operation (say A). I've few logic which has to happen before A happens and few during A happens and few after A has happened.

interface ILaunchInterface {
    public void pre();
    public void during();
    public void after();
}

class Launch implements ILaunchInterface {
    public void coreOperation() {
         pre();
         during(); 
         post();
    }

    @Override
    public void pre() {

    }

    @Override
    public void during() {
        //perform the actual operation here
    }

    @Override
    public void after() {

    }
}

Is there any better way of doing this? How to design this better?

  • What's the point in having the interface? Couldn't you just turn your pre(), during() and after() methods into private methods if you need to enforce the order in which they're called? – Ben Cottrell Dec 22 '19 at 17:24
  • but what happens when too many logic grows within pre and after? – Tom Taylor Dec 22 '19 at 17:27
  • I don't see how the interface would solve that? If the pre and after methods start to grow, then that would be the time to refactor and split those out. I'm afraid that the question and the example here doesn't really seem to illustrate or explain what the real problem is that you're actually trying to solve – Ben Cottrell Dec 22 '19 at 17:30
  • The question is quite unclear. How will the calling code look like? What do you mean by "better"? More readable? The "real" method and class names are most probably different from what you have shown us, I guess, so how shall we tell you from these contrived alias names if there is a "more readable" solution? – Doc Brown Dec 22 '19 at 17:35
  • Uff seriously no. I named it as pre and after. In pre i would play some sound when I launched an activity (in my case it's android) and during after i would broadcast some information to my clients.. – Tom Taylor Dec 22 '19 at 17:40
2

First let's fix a bug. after() is never called.

interface ILaunchInterface {
    public void pre();
    public void during();
    public void after();
}

class Launch implements ILaunchInterface {
    public void coreOperation() {
         pre();
         during(); 
         after(); // <-- note change.
    }

    @Override
    public void pre() {

    }

    @Override
    public void during() {
        //perform the actual operation here
    }

    @Override
    public void after() {

    }
}

Now let's do some refactoring to simplify.

interface ILaunchInterface {
    public void coreOperation();
}

class Launch implements ILaunchInterface {
    public void coreOperation() {
         pre();
         during(); 
         after();
    }

    private void pre() {

    }

    private void during() {
        //perform the actual operation here
    }

    private void after() {

    }
}

There, now the temporal relation between these methods is maintained because only coreOperation() has access to them.

If you wish to enforce a temporal relationship between public methods make them into functions:

interface ILaunchInterface {
    public PreResult pre();
    public DuringResult during(PreResult preResult);
    public AfterResult after(DuringResult duringResult);
}

class Launch implements ILaunchInterface {
    public void coreOperation() {
        after( 
            during(
                pre()
            )
        );
    }

    @Override
    public PreResult pre() {

    }

    @Override
    public DuringResult during(PreResult preResult) {
        //perform the actual operation here
    }

    @Override
    public AfterResult after(DuringResult duringResult) {

    }
}

There, now you have to give them what they expect when they're called in order. Keep them as pure functions and time doesn't matter. The only problem now is that these are terrible names.

Not the answer you're looking for? Browse other questions tagged or ask your own question.