2016-07-08 178 views
0

我创建了两个处理不同属性的Guice模块。这种方法的问题是我必须在实现中重复两次代码。我怎样才能避免重复,并仍然能够自定义我的绑定?如何避免重复代码

我正在考虑使用Providers,但找不到更干净的方法来做到这一点。任何方向将不胜感激

public abstract class AConfModule extends AbstractModule { 

    /** 
    * {@inheritDoc} 
    */ 
    protected void configure() { 
     // Do some Confugurations 
     iConfigure(); 
    } 

    protected abstract void iConfigure(); 

} 


public abstract class BConfModule extends AbstractModule { 

    /** 
    * {@inheritDoc} 
    */ 
    protected void configure() { 
     // Do some Confugurations 
     iConfigure(); 
    } 

    protected abstract void iConfigure(); 

} 
+2

有关移动配置和iConfigure成AbstractModule,或创建一个扩展AbstractModule另一个中间人抽象Co​​nfModule什么? – Compass

回答

4

为什么不:

public abstract class AbstractConfModule extends AbstractModule { 

    /** 
    * {@inheritDoc} 
    */ 
    protected void configure() { 
     // Do some Configurations 
     iConfigure(); 
    } 

    protected abstract void iConfigure(); 

} 

public abstract class AConfModule extends AbstractConfModule { } 
public abstract class BConfModule extends AbstractConfModule { } 
+0

有没有什么办法可以使用Provider ? – allthenutsandbolts

+0

@allthenutsandbolts你想要做什么?你为什么要使用提供者? – Bohemian

+0

我有2个模块,其中一个从命令行读取属性,并且有一个模块实际从属性文件读取并使用自定义绑定。所以我在想,如果我引入了一个提供者,那么我可以在需要时获得所需的模块类型。 – allthenutsandbolts