我应该在库存系统中使用静态列表吗?

huangapple go评论91阅读模式
英文:

Should I use a static list for my inventory system?

问题

以下是您的内容翻译:

我正在开发一个奶茶库存和订购系统,我已经为我的奶茶对象创建了一个MilkTea类。现在,我打算将所有的奶茶对象添加到一个ArrayList中。我的系统应该能够添加、编辑、显示和删除奶茶,以及订购奶茶。现在,我的问题是确定我应该实现哪种程序设计。我应该将奶茶的数组列表设置为静态吗?

  • 我的数组列表可能会随着时间的推移而改变。
  • 我的奶茶数组列表还将在与订购相关的其他类中被访问。
  • 我的数组列表不是不可变的。

到目前为止,这是我用于添加、编辑、删除和显示奶茶的类:

package inventory;

import java.util.ArrayList;

public class MilkTeaList {

    public ArrayList<MilkTea> list = new ArrayList();

    public void addMilkTea(String flavorName,String[] sizes, double[] prices, int stock) {
        list.add(new MilkTea(flavorName, sizes, prices, stock));
    }
    
    public ArrayList<MilkTea> getList() {
        return list;
    }
}
英文:

I'm developing a MilkTea Inventory and Ordering System in which I've already created a MilkTea class for my MilkTea objects. Now, I am planning to add all my MilkTea objects to an ArrayList. My system should be able to add, edit, display and delete Milktea as well as order MilkTeas. Now, my problem is determining the program design I should implement. Should I make the array list for my MilkTea, static?

  • My array list can be changed overtime.
  • My array list for milkteas will be also accessed on other class related to ordering.
  • My array list is not immutable.

So far, this is my class for adding, editing, deleting and displaying MilkTeas:

package inventory;

import java.util.ArrayList;

public class MilkTeaList {

    public ArrayList&lt;MilkTea&gt; list = new ArrayList();

    public void addMilkTea(String flavorName,String[] sizes, double[] prices, int stock) {
        list.add(new MilkTea(flavorName, sizes, prices, stock));
    }
    
    public ArrayList&lt;MilkTea&gt; getList() {
        return list;
    }
}


答案1

得分: 2

如果您正在使用数据库,我建议您在DAO类中实现常规的CRUD方法(将当前类称为MilkTeaListDao),并依赖于数据库的事务完整性。

如果没有使用数据库,将您的类命名为MilkTeaListHandlerMilkTeaListController,添加一个getInstance()方法(确保只有一个类的单个对象,通过具有私有构造函数),并使用操纵列表的方法。该列表不应为静态,方法应具有全局同步锁,以确保事务完整性。getList()方法应返回列表的副本,而不是列表本身。

我注意到您对此问题得到了一些相互矛盾的答案,所以让我为我的建议提供一些论点和解释:

  • 一开始使用可以在任何地方操纵的静态列表是方便的,但最终可能会变成一个让您在项目余生中必须处理的头疼问题。例如,假设代码的一部分正在将列表存储到磁盘(使用for循环),同时代码的另一部分(同时)正在向列表中添加、更新和删除项目。那么您不知道磁盘上存储了什么(保存还是更新先发生),并且在某个阶段(也许几年后)您会因为保存功能假定某人刚刚删除的列表中有记录而得到nullPointerExceptions。
  • 当MilkTea对象更改时,您将需要在整个代码中进行更新。例如,记得在所有地方都添加expireDate。如果将操纵列表的所有方法聚集在一个地方,那么更容易对它们进行更改(并且查看更改的影响会更容易,因为在其余代码中会得到编译错误)。
  • 如果添加/删除/更新分散在各处,您的MilkTea周围的其余逻辑可能也会分散。当逻辑发生变化时,您可能会在整个项目中找到重复的代码,并且在多个地方更新它将会很繁琐且容易出错。逻辑的一个示例是错误处理。假设系统通过从文件中读取行来更新库存,在价格设置为零时将引发异常。很好。但是对于用户可以从网络界面手动更新库存信息的功能,您忘记了添加该限制。不好。如果操纵方法在一个地方聚集,您只需对价格==零的检查进行一次,然后您就会知道它将永远生效。
英文:

If you're using a database, I suggest you implement normal CRUD methods in a DAO class (call your current class MilkTeaListDao) and rely on the database's transaction integrity.

If not, call your class MilkTeaListHandler or MilkTeaListController, add a getInstance() method (and make sure you only have one single object of the class by having a private constructor), and use methods that manipulate the list. The list should not be static and the methods should have a global synchronize lock to ensure transaction integrity. And the getList() method should return a copy of the list, not the list itself.

I notice you get a few contradicting answers to this question so let me provide a few arguments and explanations for my suggestion:

  • Using a static list that can be manipulated anywhere is convenient in the beginning of a project, but it can eventually grow into a headache you have to deal with for the rest of the project's lifetime. For example, lets say that one part of the code is storing the list to disk (using a for loop) while another part of the code (at the same time) is adding, updating and removing items in the list. Then you don't know what is stored on disk (what came first, the save or the update), and you will at some stage (maybe after several years) get nullPointerExceptions since the save feature assumes that there are records in the list that someone else has just removed.
  • When the MilkTea object changes, you will need to make updates in the entire code. For example, remember to add expireDate everywhere. If all methods that manipulate the list are gathered in one place it is easier to change them (and a lot easier to see what effects the changes have since you will get compile errors in the rest of the code).
  • If add/remove/update is spread out, it is likely that the rest of the logic surrounding your MilkTea also will spread out. When logic changes you are likely to find duplicated code throughout your project, and it will be tedious and error-prone to update it in multiple places. An example of logic is error handling. Lets say that when the system updates your stock by reading rows from a file, an exception will be thrown is the price is set to zero. Good. But for the feature where the user can manually update the stock info from a web interface, you forgot to add that limitation. Not good. If the manipulating methods were gathered in one place, you would have to do the price==zero check exactly once, and then you would know it will forever.

答案2

得分: 1

那个包含你的MilkTea实例的列表是你的“产品数据库”,用于主数据管理(创建新的MilkTea实例,修改它们或删除它们)和订单管理(订购MilkTea,生产它们并将它们交付给客户)。

由于产品数据库必须独立存在于主数据管理和订单管理中,一个static列表可以很好地替代其他地方所称的“持久化层”。但是对于一个真正高效的系统,这应该是像磁盘上的文件或真正的数据库系统这样的东西。

当然,你可以添加一个表示持久化层的类,在其中隐藏如何实际存储MilkTea实例;那个类只会有一个接口,允许你添加/替换、删除和查找MilkTea实例,就像列表一样。

英文:

That list holding your MilkTea instances is your "product database" that is used for both your master data management (creating new MilkTea instances, modifying them, or deleting them) and your order management (ordering MilkTeas, producing them, and deliver them to the client).

As the product database have to exist independently for both, the master data management and the order management, a static list would be a decent replacement for what is called a 'persistence layer' elsewhere. But for a really productive system, this has to be something like a file on disk, or a real database system.

Of course you can add a class that represents that persistence layer, and inside that, you hide how you really store your MilkTea instances; that class will just have an interface that allows you to add/replace, remove and find MilkTea instances, like the List, too.

答案3

得分: 0

是的,所以您可以在不创建新实例的情况下访问列表。如果您在另一个类中更改了任何内容,所有类和所有实例都将获得您的更改。

获取更多信息:https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

英文:

Yes, so you can access list without create a new instance. And if you change anything in another class, all classes and all instances will get your change.

For more information: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

答案4

得分: 0

这取决于您如何使用MilkTeaList
如果您只创建了一个MilkTeaList实例,那么没有理由将其设为静态。但是,如果您每次想要向列表中添加新的MilkTea时都创建新实例(这是不好的决定),那么您需要将其声明为静态。

此外,如果您将从多个线程中使用它,您应该使其具备并发安全性。

英文:

It depends on how you will use MilkTeaList.
If you creates only one MilkTeaList instance there are no reasons to make it static. But if you will create new instance each time when you want to add new MilkTea to list (this is bad desicion) so you need to declare it as static.

Also if you will use it from more than one thread you should make it concurrent-safety.

答案5

得分: 0

首先考虑一下静态的含义。当你声明静态变量时,它们在程序执行时创建,并且与它们所声明的类绑定。

这意味着如果你想要只有一个变量实例,你应该使用静态。

如果不是静态的,每次创建新对象时都需要实例化它。

请记住,如果你将这个 ArrayList 变量与无数个类共享,你可能无法得到预期的结果,因为每个人都会改变它。

英文:

First consider what static means. When you declare static variables, they are created during the time of executing the program and are bound to the class they are declared in.

Meaning if you want to have just one instance of a variable you should use static.

If it is not static you would have to instantiate it every time you create a new Object.

Keep in mind if you share this ArrayList variable with countless classes you might not get the result you expect,since everyone changes it.

huangapple
  • 本文由 发表于 2020年3月15日 17:53:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/60691618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定