2013-12-20 50 views
0

我必須維護一個代碼,爲類中的最終靜態變量添加更多的靈活性。最終靜態屬性更改..任何想法或方法?

該變量不再是一個全局常量,可以改變。

問題是該類在一個公共庫中,並在不同的項目中使用。

您是否有比將公共庫中的類代碼複製並粘貼到特定應用程序並重構它更好的方法或設計模式?

例子:

Commons項目

Class CommonClass { 

    public final static var globalSomething = somethingGlobal; 

    public static method(){ //CommonClass.globalSomething is used here} 
} 

在我的應用程序(和引用公共其他應用程序),我們可以使用靜態屬性,並調用方法:

--->var b = CommonClass.somethingGlobal;

--->var c = CommonClass.method() //we know that CommonClass.globalSomething is used here

點期望:

  • 能力改變CommonClass.somethingGlobal在我的應用程序,並採取呼叫CommonClass.method()這些變化
  • 我可以修改(添加方法)中常見的類,但我必須保持相同的初始行爲(不要打破其他項目引用的公共項目)
+0

你在用什麼IDE?在Eclipse和Intellij中有一些很棒的重構。 – david99world

+1

你的問題有點含糊。你可以添加一些代碼,或嘗試reqording你的qeustion? –

+1

我也不明白你的問題。剛刪除'final'修飾符,構建一個新版本的庫並在其他項目中使用它會出現什麼問題? –

回答

1

如果我找到了你的話,你想要實現這個參數。

看你的例子:

var c = CommonClass.method() //we know that CommonClass.globalSomething is used here 

已經有什麼不妥的地方。在調用方法之前,您不必知道必須正確設置CommonClass.somethingGlobal。這樣客戶必須知道實施,違反了信息隱藏的原則。如果需要的價值,介紹它的參數:

Class CommonClass { 

    public static void method(var globalSomething){} 
} 

另一種方法是使得無論你的變量,你的方法非靜態和使用構造函數:

Class CommonClass { 

    public var globalSomething = somethingGlobal; 

    public CommonClass(var globalSomething) { 
     this.globalSomething = globalSomething; 
    } 

    public void method(){} 
} 

PS:你的示例代碼不是java。我在回答中部分糾正了它。

相關問題