开发者

DRY: Minimizing repeated code in Java

I'm writing a method in Java:

List<Foo> computeFooList(/* arguments */)
{
    /* snip */
}

I'd like to write a second method with exactly the same logic, but a different return type:

List<String> computeStringList(/* same arguments */)
{
    /* snip */
}

I'm trying to figure out a non-hackish way to minimize the amount of repeated code between the two methods. The only logical difference between the two is that, when adding an object to the list that's returned, the first method adds the acutal Foo:

List<Foo> computeFooList(/* arguments */)
{
    List<Foo> toReturn = ...
    ...
    for (Foo foo : /* 开发者_Python百科some other list of Foo */)
    {
        if (/* some condition */)
        {
            toReturn.add(foo);
        }
    }
    ...
    return toReturn;
}

and the second adds a String representation of the Foo:

List<String> computeStringList(/* same arguments */)
{
    List<String> toReturn = ...
    ...
    for (Foo foo : /* some other list of Foo */)
    {
        if (/* some condition */)
        {
            toReturn.add(foo.toString());
        }
    }
    ...
}

In reality, it's not quite that simple. I don't want to add a Foo to toReturn unless I'm absolutely sure it belongs there. As a result, that decision is made per-foo using helper functions. With two different versions of the methods, I'd need different versions of the helper functions as well - in the end, I'd be writing two sets of nigh-identical methods, but for one little generic type.


Can I write a single method which contains all of the decision-making logic, but can generate either a List<Foo> or a List<String>? Is it possible to do this without using raw List types (bad practice in generics land!) or wildcard List<?> types? I imagine an implementation that looks something like this:

List<Foo> computeFooList(/* args */)
{
    return computeEitherList(/* args */, Foo.class);
}

List<String> computeStringList(/* args */)
{
    return computeEitherList(/* args */, String.class);
}

private List<???> computeEitherList(/* args */, Class<?> whichType)
{
    /* snip */
}

Is there any nice, elegant way to do this? I've been playing around with generic methods, but I can't see a way to do this. Even mucking about with reflection hasn't gotten me anywhere (maybe I need something like TypeToken? ...eww).


Can't you externalize transformation logic into a separate strategy (such as Guava's Function<F, T>):

public <T> List<T> computeList(/* arguments */, Function<? super Foo, T> f) {
     List<T> toReturn = ...     ...     
     for (Foo foo : /* some other list of Foo */) {
         if (/* some condition */) {
             toReturn.add(f.apply(foo));
         }
     }
     return toReturn;
} 

computeFooList:

computeList(..., Functions.identity());

computeStringList:

computeList(..., Functions.toStringFunction());


I have a "SearchFilter" interface and a "FilterAdapter" abstract class that I use in ways similar to this. The decision logic can be made independently and in a generic way from actually adding things to the return list. I would be checking each Foo and saying "true" include it or "false" exclude it.

public interface SearchFilter<T>
{
    public boolean include(T item);
    public Collection<T> apply(Collection<T> items);
}

A filter can be applied to an existing collection with the apply() method, returning a new collection that only includes the desired items.

newCollection = myfilter.apply(originalItems);

This may not be useful to you, but the include() concept should work well to avoid repeating the decision logic.

You could have a FooFilter extends FilterAdapter<Foo> (I also instantiate these anonymously in-line sometimes) which provides an implementation of include

public FooFilter extends FilterAdapter<Foo>
{
    public boolean include(Foo item)
    {
        if (item.isInvalid()) return false;
        // or any other complex logic you want
        return item.hasGoodThings();
    }
}

The apply() method is almost always just loop over the initial collection and test include so it has a default implementation in my FilterAdapter but can be overridden.


It's a little ugly, but I think this might work:

List<Foo> computeFooList(/* args */) {
    return computeEitherList(/* args */, Foo.class);
}

List<String> computeStringList(/* args */) {
    return computeEitherList(/* args */, String.class);
}

private <T> List<T> computeEitherList(/* args */, Class<T> whichType) {
    List<T> rval = new ArrayList<T>();
    for (Foo foo : listOfFoo) {
        // process foo

        if (whichType.equals(Foo.class)) {
            rval.add(whichType.cast(foo));
        }
        else if (whichType.equals(String.class)) {
            rval.add(whichType.cast(foo.toString()));
        }
        else {
            throw new SomeException("Cannot compute list for type " + whichType);
        }
    }
    return rval;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜