这个文件操作设计模式的名称是什么?

时间:2014-01-29 13:21:47

标签: java design-patterns

假设我有一个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; }
}

generateWashCode(),testHashCodeAgainst(Path)和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原则......它是否值得获得性能?),如: / p>

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是任意数据。我试图从被实例化数百万次的类中删除尽可能多的代码,并将其放在另一个单独的类中,其中包含一组静态方法,这些方法在类的实例上运行数百万次实例化。 那个设计模式有名字吗? 感谢

2 个答案:

答案 0 :(得分:2)

我认为你的“模式”基于一些误解。

  

generateHashCode(),testHashCodeAgainst(Path)和的逻辑   determineFileType()不必包含在此中   FileWrapperClass,如果有的话,可能不应该是   可能有数百万个这些FileWrapperClass对象。

您是否认为代码包含在数百万个实例中?如果是,那么你错了,代码是共享的,创建单独的静态方法来处理它们没有任何优势。

  

事实上,它不会产生更好的性能来整体化   getter / setter动态支持直接访问受保护的变量   (尽管违反OO原则......对于表演来说是值得的   获得?),如:

不,这里没有涉及性能提升。这是微观优化,无论如何都将使JIT变得不必要。

所以作为结论,抱歉,但你不是“在某事上”,但我希望你现在“走上正确的道路”。

答案 1 :(得分:1)

这听起来像Anemic Domain ModelPremature Optimization之间的组合。

相关问题