samedi 28 mai 2016

Separate logic from logging via template method pattern

May I use this template method pattern for separate logic from logging and exception handling or it is "bad practice"?

For example I have this code:

public abstract class Parent {
    private final Logger log = LoggerFactory.getLogger(getClass());

public void eat() {
    try {
        doEat();
    } catch (SomeException1 e) {
        log.debug("some text");
        throw new CustomException1();
    }
}

protected abstract void doEat();

public void sleep() {
    try {
        doSleep();
    } catch (SomeException2 e) {
        log.debug("some text");
        throw new CustomException2();
    }
}

protected abstract void doSleep();
}

And my child class:

public class Child extends Parent {
@Override
protected void doEat() {
    //some logic
}

@Override
protected void doSleep() {
    //some logic
}}

I would not have different implementations of methods doEat() and doSleep().

I want to know whether it's worth it and is it 'bad practice' or not.

Aucun commentaire:

Enregistrer un commentaire