views:

113

answers:

4

I have a class that has a big method that calls on a lot of private methods. I think I want to extract those private methods into their own classes for one because they contain business logic and I think they should be public so they can be unit tested.

Here's a sample of the code:

public void handleRow(Object arg0) {
    if (continueRunning){
        hashData=(HashMap<String, Object>)arg0;
        Long stdReportId = null;
        Date effDate=null;
        if (stdReportIds!=null){
            stdReportId = stdReportIds[index];
        }   
        if (effDates!=null){
            effDate = effDates[index];
        }
        initAndPutPriceBrackets(hashData, stdReportId, effDate);
        putBrand(hashData,stdReportId,formHandlerFor==0?true:useLiveForFirst);
        putMultiLangDescriptions(hashData,stdReportId);
        index++;
        if (stdReportIds!=null && stdReportIds[0].equals(stdReportIds[1])){
            continueRunning=false;
        }       
        if (formHandlerFor==REPORTS){
            putBeginDate(hashData,effDate,custId);
        }
        //handle logic that is related to pricemaps.
        lstOfData.add(hashData);
    }
}   

What design pattern should I apply to this problem?

+2  A: 

Hello,

The State Pattern covers this scenario quite nicely.

The details are here

Here is a C# example

Enjoy!

Doug
+1  A: 

I'd just bundle related methods and state (fields) into classes with public methods and inject them as services into the host class.

State pattern like Doug posted, describes such a division.

Also be sure to avoid cyclic dependencies when you split the behavior. This can easily happen, when done hastily.

Generally split class behavior into multiple classes for reuse and customization. If the only purpose is improved testability, then exposing methods as protected or public is an easier option.

Timo Westkämper
A: 

Your concern about unit testing could be addressed by making the methods 'protected' and putting the test classes in the same package as the original source code.

btreat
that's one angle I am considering but I have also heard that unit testing a private method is a sign of a class itching to get out.
Jeune
A: 

The dramatic refactoring you're describing solely to fit your unit-test tools is a really, really a bad idea.

Don't allow flaws in the tools to drive your design.

John
@John : Making code testable (JUnit testable for Java) is considered good design by many. What do you consider good design?
Timo Westkämper
Testability is the most desired property of code because it lets it evolve (since you can refactor it) to whatever form the best meets your needs. See also http://misko.hevery.com/code-reviewers-guide/
Itay
Yes, good testing is a critical component of good development. If the user wants to test private methods, he merely has to make the unit_test() function (or class) a friend. There is no need to refactor the object model or re-implement code simply to appease a flaw in a specific testing tool. If his testing tools can't handle well-designed code, he should consider shopping for new testing tools. :)
John