vendredi 12 juin 2015

Is 'facade method' (calling public methods of own class) considered bad design?

Is it considered bad design to have a 'facade method' (I can't think of a better term). Example:

public class MyService() {
    public void doThis() {
        // do something
    }

    public int doThat() {
       // do something
    }

    public boolean isThisTrue(String str) {
       // do something
    }

    // determine something by calling methods within the same class
    public boolean shouldSomethingBeDone() {
        int result = 0;            

        if (isThisTrue("foobar")) {
           doThis();
           result = doThat();
        }

        return result > 0; 
    }
}

I'm trying to avoid having too much logic in one of my Controller classes that is calling a service. The only way I could see getting around this was by created a method such as the one above.

The dilemma arose when I tried to create a unit test and mock the calls within the shouldSomethingBeDone() method. Since all of the calls are to methods within the same class it's difficult to mock this.

Thanks.

Aucun commentaire:

Enregistrer un commentaire