繁体   English   中英

Java代码重构问题

[英]Java code refactoring issue

我有一个相似的类结构,如下所示:

       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);
        }

我如何重构for循环? (for循环稍微复杂一点。逻辑总是一样。列表的迭代,对象的创建以及将对象添加到列表是不同的。)

如果所有功能都是独立的,那么您唯一可以重构的就是将通用代码放入私有功能中

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

然后在所有循环中使用它

for (Obj e : getFooSpecObj()) {
       Foo foo = new Foo(.., .., ..);
       doCommonThings(e);
       wrap.getFooList().add(foo);
}

如果您的代码确实像上面的代码一样简单,那么您可能会发现,为简化代码所做的任何重构都只会使其变得更加复杂。

由于缺少实现的某些部分,因此很难判断重构方向。 这是我的猜测,在这种情况下如何进行:

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
            }
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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