English 中文(简体)
贾瓦——如果我用一个领域或一个类别来处理这一问题:
原标题:Java - Should I use a field or a class for the problem as follows:

我在非行有两张表格。

  • 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)

我们可以看到,两个表格都通过链接。 外国钥匙(燃料_brand_id)

现在,我想设计一个与上述数据模型相对应的物体模型,但我感到困惑的是,从以下两种方法中采用哪种方法:

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

页: 1

  • 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.

问题1. 哪一种办法更好?

问题2. 每一种做法的利弊是什么?


Upto My Knowledge:

办法2

  • 它使我们的物体模式更加 gr。

  • 就良好设计原则而言,我们的物体-模型必须作为同体-模型的微粒。 方法2遵循这一原则。

办法1

  • No need to create a separate object for FuelBrand for every 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.

这两种办法之间没有明确的普遍选择,因为许多办法取决于具体情况。 一些决定因素:

  • 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.

作为底线,第2条办法更为灵活,因此,我更希望,即使现在没有可预见的变化,显然需要这一设计——在全系统发展中,从来就永远不会说:-)

问题回答

哪一种情况较好取决于你想要与信息做些什么。

a) 如果要列出所有提供某种品牌的站点,你可能也想选择从燃料Brand名称到收集燃料结构的某种地图。

b) 如果燃料品牌拥有或将拥有你想要使用的任何财产(价格等),那么最好将其分类。

无需为每个燃料站建立一个单独的燃料舱标

这两种方法都有相同数目的物体——你有NensBrand物体或Nstring物体,N有的是品牌数量,有的则取决于是否重复使用这些物体。 因此,这一论点是可疑的。

我倾向于以一个类别代表每个桌子,因为它的确明确表明品牌是一种品牌,而不是另一种东西。 它也为不同的品牌提供相同的名称,而关系图谱法则允许这样做,尽管其可能性不大。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签