简体   繁体   中英

Making intelligent use of bufferReader

I have some design pattern problem in 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.

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:

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

For more information on this topic, I highly recommend reading Effective Java by Josh Bloch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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