比方说,我有一个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是任意数据段。我试图从实例化数百万次的类中移除尽可能多的代码,并将其放入另一个单例类中,并使用一组静态方法来处理实例化数百万次的类的实例。 是否有该设计模式的名称? 谢谢
为什么外面的人能够调用'generateHashCode'或'setHashCode'?这个类可以自己做(一旦有人需要hashCode /一旦有人设置路径)并封装这些东西。 – zapl
没有冒犯,但基于“public void setPathFromUserInput(JFrame whatever){...}”您可能不会太多。从JFrame获取路径相当不错。 – Kayaman
@zapl它也可以被保护,没关系。 hashCode和fileType是任意多余的数据。更多的想法是我试图从FileWrapperClass中移除所有可能的代码,这样每个实例都会消耗尽可能少的内存 – Inversus