简体   繁体   English

哪种方法更好? 静态方法里面的ArrayList很混乱

[英]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. Web应用程序将从每个http表单提交中接收String文本。 Each form submission is text containing item IDs with free text created by users. 每个表单提交都是包含项目ID的文本,其中包含用户创建的自由文本
  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. 在解析时,将创建本地ArrayList,并将此本地ArrayList用作项目的临时占位符。 Because of this ArrayList, I am not sure if static method is appropriate or not. 由于这个ArrayList,我不确定静态方法是否合适。
  3. Using this ArrayList, text will be reformatted. 使用此ArrayList,将重新格式化文本。 ArrayList will be loop through and formatted with some text. ArrayList将循环并使用一些文本格式化。

Usually I would create simple static method take String as input and return formatted text. 通常我会创建简单的静态方法,将String作为输入并返回格式化文本。 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. 但由于ArrayList被用作局部变量,我不确定静态方法是否是正确的选择,还是应该为它创建普通的对象实例。 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? 由于ArrayList是局部变量,但最好将其设计为成员变量并将类实例化为对象,输入文本作为成员变量而不是静态方法?

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 在servlet层,它将用作以下内容

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

Keep in mind: static can be seen as abnormality for good OO. 请记住: 静态可被视为良好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. 这是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. 但是您显示的代码并不依赖于共享数据 - 因为它没有使用任何静态字段。

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

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