我在某处读到,开发人员应该始终开发接口(interface),然后实现该接口(interface)。我想我只是想学习这些构建器模式......但这是我的用例......
我有一个中心类...说ProcessFile
现在,我正在编写它可以处理的两种格式,一种是 csv,另一种是 json。
如何设计这些类。
我猜它会是这样的:
public interface CustomFormat{
//couple of signatures
}
public class csv implements CustomFormat{
}
public class json implements CustomFormat{
}
public class ProcessFiles{
//somehow uses that CustomFormat interface??
}
实现这个的最佳方法是什么?
最佳答案
以 javaPlease42 的答案为基础。我会将 ProcessFiles 重命名为 FileProcessor 或类似的名称。一个好的经验法则是使用名词来命名类,并使用动词来描述名词的功能来命名方法。下面是它的外观示例。
public class FileProcessor {
public void process(CustomFormat file) {
// pass in the interface, that way you can use any implementing class
// like csv or json
}
}
关于java - 理解java中的设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19610546/