2014-01-29 93 views
0

比方說,我有一個Path對象的包裝類,我想要存儲一些其他信息:例如hashCode和fileType。這個文件操作設計模式的名稱是什麼?

public class FileWrapperClass { 
    private Path thePath; 
    private String hashCode; 
    private String fileType; 
    ... 
    public void setPathFromUserInput(JFrame whatever) { ... } 
    public void generateHashCode() { ... } 
    public boolean testHashCodeAgainst(Path testAgainstMe) { ... } 
    public void determineFileType() { ... } 
    ... 
    public Path getPath() { return thePath; } 
    public void setPath(Path thePath) { this.thePath = thePath; } 
    public String getHashCode() { return hashCode; } 
    public String getFileType() { return fileType; } 
} 

爲generateHashCode(),testHashCodeAgainst(路徑),並determineFileType()的邏輯沒有被包括在這個FileWrapperClass,並且可能不應該,如果有將是潛在的數以百萬計的這些FileWrapperClass對象。

所以,我想我可能會把它拉到另一個類並使它們成爲對FileWrapperClass實例進行操作的靜態方法。這讓我使出渾身但最基本的getter和setter方法,就像這樣:

public class FileWrapperClass { 
    private Path thePath; 
    private String hashCode; 
    private String fileType; 
    ...  
    public Path getPath() { return thePath; } 
    public void setPath(Path thePath) { this.thePath = thePath; } 
    public String getHashCode() { return hashCode; } 
    public void setHashCode(String hashCode) { this.hashCode = hashCode; } 
    public String getFileType() { return fileType; }  
    public String setFileType(String fileType) { this.fileType = fileType; } 
} 

public class FileWrapperClassOperator { 
    public static void setPathFromUserInput(JFrame whatever) { ... } 
    public static void generateHashCode() { ... } 
    public static boolean testHashCodeAgainst(Path testAgainstMe) { ... } 
    public static void determineFileType() { ... } 
} 

其實,是不是產生更好的性能尼克斯整體的getter/setter動態贊成保護的直接訪問變量(儘管破OO原則..是值得的性能增益?),如:

public class FileWrapperClass { 
    public Path thePath; 
    public String hashCode; 
    public String fileType; 
    ...  
} 

public class FileWrapperClassOperator { 
    public static void setPathFromUserInput(JFrame whatever) { ... } 
    public static void generateHashCode() { ... } 
    public static boolean testHashCodeAgainst(Path testAgainstMe) { ... } 
    public static void determineFileType() { ... } 
} 

我覺得我在這裏的東西。如果它是現有的設計模式,那麼我想知道它是什麼,以便我可以學習它並對其進行編碼。如果不是,那麼任務就是優化這個概念。

要清楚的是,hashCode和fileType是任意數據段。我試圖從實例化數百萬次的類中移除儘可能多的代碼,並將其放入另一個單例類中,並使用一組靜態方法來處理實例化數百萬次的類的實例。 是否有該設計模式的名稱? 謝謝

+0

爲什麼外面的人能夠調用'generateHashCode'或'setHashCode'?這個類可以自己做(一旦有人需要hashCode /一旦有人設置路徑)並封裝這些東西。 – zapl

+1

沒有冒犯,但基於「public void setPathFromUserInput(JFrame whatever){...}」您可能不會太多。從JFrame獲取路徑相當不錯。 – Kayaman

+0

@zapl它也可以被保護,沒關係。 hashCode和fileType是任意多餘的數據。更多的想法是我試圖從FileWrapperClass中移除所有可能的代碼,這樣每個實例都會消耗盡可能少的內存 – Inversus

回答

2

我想你基於你的「模式」的一些誤解。

爲generateHashCode(),testHashCodeAgainst(路徑),並 determineFileType()的邏輯沒有被包括在這個 FileWrapperClass,並且可能不應該,如果有將要 數百萬潛在這些FileWrapperClass對象。

您是否認爲代碼包含在數百萬個實例中的每一箇中?如果是的話,那麼你錯了,代碼是共享的,創建單獨的靜態方法來處理它們是沒有好處的。

其實,是不是產生更好的性能尼克斯整個 的getter/setter動態有利於保護變量直接訪問 (儘管打破OO原則..是值得的性能增益 ?),如:

不,這裏沒有涉及性能增益。這是微型優化,無論如何JIT將不必要的。

所以作爲一個結論,抱歉,但你並沒有在這裏「接近某件事物」,但我希望你現在「走在正確的道路上」。