简体   繁体   中英

Structuring a class

I have a couple methods that need to run sequentially. They all live in the same class. Is it better to structure them like this?

public class Test{

    public Test(){
        foo1();
    }

    private void foo1(){
        //do stuff
        foo2();
    }

    private void foo2(){
        //do stuff
        foo3();
    }

    private void foo3(){
        //do stuff
    }

}

Or this.

public class Test{

    public Test(){
        doFoo();
    }

    private void doFoo(){
        foo1();
        foo2();
        foo3();
    }
    private void foo1(){
        //do stuff
    }

    private void foo2(){
        //do stuff
    }

    private void foo3(){
        //do stuff
    }

}

Which way would be more clear? I'm leaning towards the second example, but i'm not sure if having a method that only calls other methods is best practice.

I think it depends on the operations. If operation foo2 is part of what foo1 does, then you should call foo2 from foo1.

If foo1 and foo2 are unrelated and only part of the foo operation, then call these two from foo.

It depends on the semantics of the methods which call hierarchy is clearer and therefore easier to understand.

The second example is better for multiple reasons.

First, you can write unit tests to test foo1 , foo2 and foo3 . With the first method you can only do a unit test for foo1 .

Second, the second solution is better if in the future you have to change the order of your calls.

That completely depends on what these methods do.

If they are really three steps that need to be done in sequence then version 2 is better.

But if they are "nested" steps where foo2 is part of what needs to be done in foo1 then version 1 is better.

Example for the second case: writeJavaBeanToFile transforms the bean to a Map, then calls writeJavaMapToFile which transforms the Map to a String and calls writeStringToFile .

I can see both of the methods have their pros and cons. As far as readability goes the second example is much easier to follow. There is one example where the first one might be useful though.

public class Test{

    public Test(){
        foo1();
    }

    private int foo1(){
        //do stuff
        if(something went wrong) return -1;
        foo2();
        return 0;
    }

    private int foo2(){
        //do stuff
        if(something went wrong) return -1;
        foo3();
        return 0;
    }

    private int foo3(){
        //do stuff
        if(something went wrong) return -1;
        return 0;
    } 

}

Depending on wheter you want to execute foo3 regardless whether or not an error occurs the second example would be better. If you only want to execute foo3 if foo2 is successful the first method might be useful.

You should follow second method which is also a pattern - facade pattern. You basically want to do 3 operations in sequence and your invocation is made much simpler if you map these 3 to 1 method. This is what facade pattern also does - easing up the invocation interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM