0

I have a similary class structure as below:

       public class Foo extends Base{
                ...
        }   

        public class Bar extends Base{
                ...
        }

        public class Aos extends Base{
                ...
        }

        public class Wrap{
            List<Foo> getFooList();
            List<Bar> getBarList();
            List<Aos> getAosList();
        }

Fold fooFold = getFooFold();
Fold barFold = getBarFold();
Fold aosFold = getAosFold();

    // Code to refactor
        for (Obj e : fooFold.getObj()) {
               Foo foo = new Foo(.., .., ..);
               for (Som s: e.getSom())) {
                    doSom();
               }
               wrap.getFooList().add(foo);
        }

        for (Obj e : barFold.getObj()) {
               Bar bar = new Bar(.., .., ..);
               for (Som c : e.getSom())) {
                    doSom();
               }
            wrap.getBarList().add(bar);
        }

        for (Obj e : aosFold.getObj()) {
             Aos aos = new Aos(.., .., ..);
             for (Som c : e.getSom())) {
                    doSom();
            }
               wrap.getAosList().add(aos);
        }

How can i refactor the for-loops? (The for-loops are a "little" more complex. The logic do always the same. The iteration over the list, the creation of the object and adding the object to the list are different. )

4

3 回答 3

1

If all the functions are separate, the only thing you can refactor is put the common code in a private function

private doCommonThings(Base e) {
      for (Som c : e.getSom())) {
            doSom();
       }
}

then use it in all your loops

for (Obj e : getFooSpecObj()) {
       Foo foo = new Foo(.., .., ..);
       doCommonThings(e);
       wrap.getFooList().add(foo);
}
于 2013-10-23T08:41:12.630 回答
0

If your code is really as simple as you have it above, you will probably find that any refactoring that you do to simplify it, will only end up making it more complex.

于 2013-10-23T08:38:02.160 回答
0

Here is hard to judge about re-factoring direction as some parts of implementation are missing. Here is my guess how to proceed in this case:

public class ListBase< T >  {

    private List< T > _list;

    public ListBase(ListFactory< List < T > > list_factory ){
       _list = ( List< T > ) list_factory.create ( );
    }

    public void foreach ( CallbackInterface< T > callback ){
       for ( T i :getList( ) ){
           callback.process ( i );
       }
    }

    public List < T > getList ( ){
       return _list;
    }
}

public interface ListFactory< T >  {
    List< T > create ();
}

public interface CallbackInterface < I > {
    void process ( I element );
}

class ListFactorryArrayList < T > implements ListFactory < T > {
    @Override
    public ArrayList< T > create( ) {
        return new ArrayList < T > ( );
    }
}
public class Wrap {

    public ListBase< Foo > _listFoo = new ListBase<Foo >( new ListFactorryArrayList < List<Foo> > () );
    public ListBase< Bar > _listBar = new ListBase<Bar >( new ListFactorryArrayList < List<Bar> > () );
}

public class Main {

    public static void main(String[] args) {
    Wrap w = new Wrap ();
        w._listFoo.getList().add( new Foo () );
        w._listFoo.foreach( new CallbackInterface <Foo > () {

            @Override
            public void process(Foo element) {
                // do smth with foo object
            }
        });
    }
}
于 2013-10-23T10:36:33.910 回答