简体   繁体   English

Java中具有相同字段的两个类的面向对象设计

[英]Objects Oriented Design for Two Classes with the same Fields in Java

I have two classes called ShoppingCart and StoreInventory. 我有两个类,分别称为ShoppingCart和StoreInventory。 These are used in a convenience store system, which involves the business domain of retail. 这些用于便利店系统,该系统涉及零售的业务领域。

They both contain a List products field. 它们都包含“列出产品”字段。 However, only the ShoppingCart should be able to calculate its own totalCost of all products in the list. 但是,只有ShoppingCart应该能够计算列表中所有产品自己的totalCost。 Also, these two are suspiciously ENTIRELY different objects but have the same fields (addendum : and METHODS, except ShoppingCart can calculate its total value). 同样,这两个对象可疑是完全不同的,但是具有相同的字段(附录:和方法,除了ShoppingCart可以计算其总价值)。

What are the best ways thru interfaces and inheritance can this be solved? 通过接口和继承可以解决的最佳方法是什么?

Here is my understanding of the issue, please correct me if I am wrong. 这是我对问题的理解,如果我错了,请纠正我。

You have an inventory class, and every item which can be sold in the store is kept as an instance of this class. 您有一个库存类,并且可以在商店中出售的每个商品都作为该类的一个实例保留。 You also have a shopping cart class. 您也有一个购物车课程。 Shopping cart contains a list of the products it contains. 购物车包含其包含的产品列表。 Shopping cart can also calculate the combined price of all of its items. 购物车还可以计算其所有项目的组合价格。

If this is true, I have a few suggestions. 如果是这样,我有一些建议。

  • I would suggest creating another class, "item" to store an individual item in the store. 我建议创建另一个类“项目”以在商店中存储单个项目。 This allows store inventory to be a list of items. 这使商店库存成为项目清单。 Item can have fields for price, and potentially information about stock and vendors. 项目可以具有价格字段,以及有关库存和供应商的潜在信息。
  • I would suggest a driver class to handle the creation/modification of shopping carts, and the modification of store inventory, however you decide to handle it. 我建议使用驱动程序类来处理购物车的创建/修改和商店库存的修改,但是您决定处理它。 It may even make sense to have one driver for the customer (shopping carts) and one for the owner/employees (inventory/items). 甚至一位顾客(购物车)和一位老板/雇员(库存/物品)的司机甚至是有意义的。

If you did want to use inheritance, you could write up an item list class, which would create a list of items, and contain methods for add, remove, modify, etc. Then you could write shopping cart and store inventory as subclasses of this, with each extending item list, but having their own methods as well, for example, the price summation in shopping cart. 如果您确实想使用继承,则可以编写一个项目列表类,该类将创建项目列表,并包含添加,删除,修改等方法。然后,您可以编写购物车并将库存存储为该类的子类。 ,其中包含每个扩展的商品列表,但也有自己的方法,例如购物车中的价格总和。 This becomes a bad idea if you want to have a separate item class for shoppers and employees. 如果您想为购物者和员工提供单独的商品类,那么这将是一个坏主意。 For example, the one for shopper includes quantity bought and price, while the one for employees includes quantity in stock and vendor information as well as price. 例如,用于购物者的一个包括购买的数量和价格,而用于雇员的一个包括库存数量,供应商信息以及价格。 In this case you would need multiple different item classes, and the inheritance would no longer work. 在这种情况下,您将需要多个不同的项目类,并且继承将不再起作用。

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

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