简体   繁体   English

智能使用bufferReader

[英]Making intelligent use of bufferReader

I have some design pattern problem in java. 我在Java中遇到一些设计模式问题。 I have following method 我有以下方法

public HashMap<String, Integer> createFrequentVocabs(bufferedReader buffr1,bufferedReader buffr2,bufferedReader buffr3){
    BufferedReader = new BufferedReader(new FileReader(new File(file)));
    HashMap<String, Integer> hm1 = new HashMap<String, Integer>();
    HashMap<String, Integer> hm2 = new HashMap<String, Integer>();
    String strngArry = new String();
    hm1 = getValue1(buffr1);
    hm2 = getValue2(buffr2);
    strngArray = getValue3(buffr3);
    return hm;
}

All the buffer Are from same text file. 所有缓冲区均来自同一文本文件。 This looks little bit ugly , how would i make it little bit beauftiful. 这看起来有点丑陋,我将如何使其变得美丽。 I would like to pass buffer once in the method or is there any way to pass file path and creating buffer inside the method itself. 我想在方法中传递一次缓冲区,或者有什么方法可以传递文件路径并在方法本身内部创建缓冲区。 Any kind of suggestion would be appreciated. 任何形式的建议将不胜感激。

I'm not entirely sure what this method is supposed to do. 我不完全确定该方法应该做什么。 Can you elaborate further? 您能否进一步阐述?

However, if you only wanted to reduce the number of method arguments to this method and create the BufferedReaders internally, one possibility would be to pass an array of Path objects. 但是,如果只想减少此方法的方法参数数量并在内部创建BufferedReaders,则一种可能性是传递Path对象数组。

As a side note, it is also preferable to use the interface of an object over it's concrete implementation if you don't need any of the underlying concrete object's functionality. 附带说明一下,如果您不需要基础具体对象的任何功能,则最好在其具体实现上使用对象的接口。 This allows you to swap the implementation to something different should the need arise without changing the contract of your method to users of your code. 这样,您可以在需要时将实现交换到其他内容上,而无需更改与代码用户的方法约定。

For example, rather than writing: 例如,而不是写:

HashMap<String, Integer> hm1 = new HashMap<String, Integer>();

You would write: 你会这样写:

Map<String, Integer> hm1 = new HashMap<String, Integer>();

Or, in Java 7: 或者,在Java 7中:

Map<String, Integer> hm1 = new HashMap<>();

For more information on this topic, I highly recommend reading Effective Java by Josh Bloch. 有关此主题的更多信息,我强烈建议阅读Josh Bloch撰写的Effective Java

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

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