简体   繁体   中英

Which approach is better? ArrayList inside static method is confused

My major concern is thread-safe to implement following scenario and I am trying to see if using static method better than normal object instance or vice versa. I read some articles but i got confused more.

  1. Web application will receive String text from each http form submission. Each form submission is text containing item IDs with free text created by users.
  2. String text will be parsed. while parsing, local ArrayList will be created and this local ArrayList is used as a temporary placeholder for items. Because of this ArrayList, I am not sure if static method is appropriate or not.
  3. Using this ArrayList, text will be reformatted. ArrayList will be loop through and formatted with some text.

Usually I would create simple static method take String as input and return formatted text. But because of ArrayList is used as a local variable, I am not sure if static method is right choice or should I create normal object instance for it. Since ArrayList is local variable but is it better to design it as member variable and to instantiate class as object with input text as member variable than static method?

Following class is what I did, but not sure if I should rewrite as a non-static.

public class A {

 private static List<Item> parseFreeText(String message){

       List<Item> itemList = new ArrayList<Item>();
       /*
       read message line by line and parse it and build ArrayList and return

       itemList.add(item);
       */

       return itemList;
 }     

 public static String formattedTextOfItems(String message){

    List<Item> itemList = parseFreeText(message);       
    StringBuilder sb = new StringBuilder();
    for(Item item: itemList){
       /* format string with item information */
       sb.append("ITEM ID: " + item.getId());
    }
    return sb.toString();
 }

and at servlet layer it will be used as following

 String order = A.formattedTextOfItems(req.getParameter("orderText")

Keep in mind: static can be seen as abnormality for good OO.

Using static means to introduce tight coupling between your classes; and even worse: you "kill" polymorphism; which is one of the cornerstones of OO.

In that sense: the default is not make things static. Exceptions could be helper / utility classes that have a very defined/clear scope. So you are OK from that perspective.

Beyond that: no worries. Code becomes sensitive to threads when you have shared data that multiple threads manipulate. In your example - you dont have that: each thread that invokes your methods ... will create its own list instance!

Meaning: if your code would look like:

public class A {
  private List<String> itemList;

and that static field would then be used by your different methods; then you would be facing multi-threading issues. But the code you are showing isn't relying on shared data - because it is not using any static fields.

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