Java - 我应该使用字段还是类来解决问题,如下所示:

发布于 2024-09-07 01:11:19 字数 920 浏览 0 评论 0原文

我在数据库中有两个表

  • FuelStation(fuel_station_id:int(PK),fuel_station_name:varchar,fuel_brand_id:int(FK))
  • FuelBrand(fuel_brand_id:int(PK), Fuel_brand_name: varchar)

正如我们所看到的,两个表都是通过链接的。外键 (fuel_brand_id)

现在,我想设计一个与上述数据模型相对应的对象模型,但我很困惑使用以下两种方法:

  • 是否只创建一个类 FuelStation 并将燃料品牌存储为 FuelStation 类中的 String

或者

  • 创建两个类:FuelStationFuelBrand。然后将它们与多对一关系关联起来,因为一个加油站将只有一个燃料品牌,但有一个燃料品牌可以有许多加油站

Q1.哪种方法更好?

Q2。每种方法的优点和缺点是什么?


据我所知:

方法 2 的优点

  • 它使我们的对象模型更加精细。

  • 根据。为了良好的设计原则,我们的对象模型必须至少与关系模型一样细粒度。方法2遵循这个原则

方法 1 的优点

  • 无需为每个FuelStation创建单独的FuelBrand对象

I have two tables in the DB

  • FuelStation (fuel_station_id: int (PK), fuel_station_name: varchar, fuel_brand_id: int(FK))
  • FuelBrand (fuel_brand_id: int (PK), fuel_brand_name: varchar)

As we can see, both tables are linked via. foreign key (fuel_brand_id)

Now, I want to design an object model corresponding to the above data model but I am confused which approach to use from the following two approaches:

  • Whether to make only one class FuelStation and store fuel brand as String in the FuelStation class.

OR

  • Make two classes: FuelStation and FuelBrand. And then associate them with a Many-to-One relationship as one fuel station will have only one fuel brand but one fuel brand can have many fuel stations.

Q1. Which approach is better?

Q2. What are the pros and cons of each approach?


Upto My Knowledge:

Pros of Approach 2

  • It has made our object model more granular.

  • Acc. to good design principles, our object-model must be atleast as granular as relational-model. And approach2 follows that principle

Pros of Approach 1

  • No need to create a separate object for FuelBrand for every FuelStation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

菊凝晚露 2024-09-14 01:11:19
  1. 仅创建一个类 FuelStation,并将燃料品牌存储为 FuelStation 类中的字符串。
  2. 创建两个类:FuelStation 和 FuelBrand。然后将它们与多对一关系关联起来,因为一个加油站只能有一个燃油品牌,但一个燃油品牌可以有多个加油站。

这两种方法之间没有明确的普遍选择,因为很大程度上取决于具体情况。一些决定因素:

  • 您有多少个电台和品牌?如果您有大量加油站使用相同的燃料品牌,最好使用第二种方法来避免重复。 OTOH 如果实际上每个燃料品牌仅由一个加油站使用(并且不会改变),则使用第一种方法是有意义的。
  • FuelBrand 属性是否可能/预期发生变化,或者引入新属性?如果是这样,最好还是使用第二种方法 - 否则 FuelBrand 中的任何更改都需要更改 FuelStation 类。
  • 未来一个加油站有可能有多个燃油品牌吗?这又需要第二种方法。

作为底线,第二种方法更灵活,所以我更喜欢这种方法,即使现在没有可预见的变化明确需要这种设计 - 在软件开发中,永远不要说永远:-)

  1. make only one class FuelStation and store fuel brand as String in the FuelStation class.
  2. make two classes: FuelStation and FuelBrand. And then associate them with a Many-to-One relationship as one fuel station will have only one fuel brand but one fuel brand can have many fuel stations.

There is no clear universal choice between these two approaches, since a lot depends on the context. Some deciding factors:

  • how many stations and brands do you have? If you have a huge number of stations using the same fuel brand, it may be better to avoid duplication using the 2nd approach. OTOH if in practice each fuel brand is used by only one station (and it's not going to change), it makes sense to use the 1st approach.
  • is it possible/expected that FuelBrand properties change, or new properties are introduced? If so, it is again better to use the 2nd approach - otherwise any change in FuelBrand would require changing the FuelStation class.
  • is it possible that a fuel station may have multiple fuel brands in the future? That would again require the 2nd approach.

As a bottom line, the 2nd approach is more flexible, so I would prefer that, even if there is no foreseeable changes right now which would explicitly require this design - in SW development, never say never :-)

木落 2024-09-14 01:11:19

哪一个更好取决于您想如何处理这些信息。

a) 如果有兴趣列出提供特定品牌的所有加油站,您可能还希望选择从 FuelBrand 名称到 FuelStations 集合的某种地图。

b) 如果燃料品牌具有或将具有您想要使用的任何属性(价格等),最好从中创建一个类。

Which one is better depends on what you want to do with the information.

a) If it is interesting to list all stations that deliver a certain brand, you might also want to opt having some sort of map from the FuelBrand name to a collection of FuelStations.

b) If the fuel brand has or will have any properties (price etc. ) that you want to use, it is better to make a class out of it.

美男兮 2024-09-14 01:11:19

无需为每个 FuelStation 的 FuelBrand 创建单独的对象

这两种方法都具有相同数量的对象 - 您要么有 N 个 FuelBrand 对象,要么有 N 个字符串对象,N 是品牌数量或加油站数量,具体取决于您是否重复使用对象与否。所以这个论点是似是而非的。

我倾向于用一个类来表示每个表,因为它确实明确表明该品牌是一个品牌而不是另一个东西。它还迎合具有相同名称的不同品牌,这是关系模式允许的,尽管可能性不大。

No need to create a separate object for FuelBrand for every FuelStation

Either approach has the same number of objects - you either have N FuelBrand objects, or N string objects, N being either the number of brands or the number of stations depending whether you reuse the objects or not. So that argument is specious.

I would tend to represent each table with an class, since it does make it explicit that the brand is a brand rather than another thing. It also caters for different brands with the same name, which the relational schema allows, even though it's not very likely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文