Java代码重构问题

Java代码重构问题,java,refactoring,Java,Refactoring,我的班级结构如下: public class Foo extends Base{ ... } public class Bar extends Base{ ... } public class Aos extends Base{ ... } public class Wrap{

我的班级结构如下:

       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);
        }
公共类Foo扩展了基{
...
}   
公共类栏扩展了基{
...
}
公共类Aos扩展了基本类{
...
}
公共类包装{
列出get傻瓜列表();
List getBarList();
列出getAosList();
}
Fold foopold=getfoopold();
Fold barFold=getBarFold();
Fold aosFold=getAosFold();
//要重构的代码
for(Obj e:foopold.getObj()){
Foo-Foo=新的Foo(…,…);
for(Som s:e.getSom()){
doSom();
}
wrap.get傻瓜列表().add(foo);
}
for(对象e:barFold.getObj()){
棒材=新棒材(…,…,…);
for(Som c:e.getSom()){
doSom();
}
wrap.getBarList().add(bar);
}
for(对象e:aosFold.getObj()){
Aos Aos=新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
            }
        });
    }
}
公共类列表库{
私有列表\u列表;
公共列表库(列表工厂>列表工厂){
_list=(list)list_factory.create();
}
公共void foreach(CallbackInterfacecallback){
for(ti:getList()){
程序(i);
}
}
公共列表getList(){
返回列表;
}
}
公共接口列表工厂{
列表create();
}
公共接口回调接口{
无效过程(I要素);
}
类ListFactoryrArrayList实现ListFactory{
@凌驾
公共阵列列表创建(){
返回新的ArrayList();
}
}
公共类包装{
public ListBase\u listFoo=new ListBase(new listfactorryarylist());
public ListBase\u listBar=new ListBase(new listfactorryarylist());
}
公共班机{
公共静态void main(字符串[]args){
Wrap w=新的Wrap();
w、 _listFoo.getList().add(new Foo());
w、 \u listFoo.foreach(新的回调接口(){
@凌驾
公共作废流程(Foo元素){
//使用foo对象执行smth
}
});
}
}

是否必须分别调用
getFooSpecObj()
getAosSpecObj()
getBarSpecObj()
?在这种情况下,必须有3个for循环。除此之外,将嵌套的for提取到一个私有函数中是的,它们必须单独调用。那么,你到底想重构什么?你能发布一些编译代码吗?对doSom()的调用在循环中-据我所知,它们不使用当前的Som迭代,那么它们用于什么呢?如果是c.doSom()或doSom(c),则更有意义。另外,我希望这些不是您真正的变量名。变量名应该是描述性的。