Java - 我应该使用字段还是类来解决问题,如下所示:
我在数据库中有两个表
- 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
。
或者
- 创建两个类:
FuelStation
和FuelBrand
。然后将它们与多对一关系关联起来,因为一个加油站将只有一个燃料品牌,但有一个燃料品牌可以有许多加油站。
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 asString
in theFuelStation
class.
OR
- Make two classes:
FuelStation
andFuelBrand
. 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这两种方法之间没有明确的普遍选择,因为很大程度上取决于具体情况。一些决定因素:
FuelBrand
属性是否可能/预期发生变化,或者引入新属性?如果是这样,最好还是使用第二种方法 - 否则FuelBrand
中的任何更改都需要更改FuelStation
类。作为底线,第二种方法更灵活,所以我更喜欢这种方法,即使现在没有可预见的变化明确需要这种设计 - 在软件开发中,永远不要说永远:-)
There is no clear universal choice between these two approaches, since a lot depends on the context. Some deciding factors:
FuelBrand
properties change, or new properties are introduced? If so, it is again better to use the 2nd approach - otherwise any change inFuelBrand
would require changing theFuelStation
class.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 :-)
哪一个更好取决于您想如何处理这些信息。
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.
这两种方法都具有相同数量的对象 - 您要么有 N 个 FuelBrand 对象,要么有 N 个字符串对象,N 是品牌数量或加油站数量,具体取决于您是否重复使用对象与否。所以这个论点是似是而非的。
我倾向于用一个类来表示每个表,因为它确实明确表明该品牌是一个品牌而不是另一个东西。它还迎合具有相同名称的不同品牌,这是关系模式允许的,尽管可能性不大。
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.