繁体   English   中英

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

[英]What is the name of this file operation design pattern?

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

这个FileWrapperClass中不必包含generateHashCode(),testHashCodeAgainst(Path)和defineFileType()的逻辑,如果可能会有数百万个此类FileWrapperClass对象,则可能不需要。

因此,我想到我可能会将其拉到另一个类,并使它们成为可在FileWrapperClass实例上运行的静态方法。 这使我可以抽出除最基本的吸气剂和吸气剂以外的所有产品,例如:

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() { ... }
}

实际上,通过直接访问受保护的变量(尽管违反了OO原则。是否值得为了获得性能而值得吗?),从而使整个getter / setter动态无法实现时,它不会产生更好的性能,例如:

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是任意数据。 我试图从被实例化数百万次的类中删除尽可能多的代码,并将其放入具有多个静态方法的另一个单例类中,这些静态方法对被实例化数百万次的类的实例进行操作。 该设计模式有名称吗? 谢谢

我认为您是基于某些误解而建立了“模式”。

这个FileWrapperClass中不必包含generateHashCode(),testHashCodeAgainst(Path)和defineFileType()的逻辑,如果可能会有数百万个此类FileWrapperClass对象,则可能不需要。

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

实际上,通过直接访问受保护的变量(尽管违反了OO原则。是否值得为了获得性能而值得吗?),从而使整个getter / setter动态无法实现时,它不会产生更好的性能,例如:

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

总结一下,对不起,但是您现在不在这里,但是我希望您现在在“正确的道路上”。

对我来说,这听起来像是贫血领域模型过早优化的结合。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM