类设计,哪一种是最好的设计方法?

发布于 2024-10-12 17:35:24 字数 194 浏览 2 评论 0原文

我是架构设计的新手,需要一些帮助。

我有两个班级,即“零件”和“供应商”。一部分会有供应商。

在我的类设计中,我应该将“int SupplyID”(类型为“int”)或“供应商供应商”(类型为“Supplier”)作为零件类中的属性吗?

哪一个更好?它们的优点和缺点是什么?

Kinldy 对此提出您的意见。

I am new to architecture design and need some help on this.

I have two class namely 'Part' and 'Supplier'. A part will have supplier.

In my class design, should i have 'int SupplierID' (type is 'int') or 'Supplier supplier' (type is 'Supplier' ) as my property in Part class ?

Which one is better? What is the Pros and Cons of them?

Kinldy provide your input on this.

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

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

发布评论

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

评论(1

陌上芳菲 2024-10-19 17:35:24
Supplier supplier 

将Supplier 作为一种类型并将SupplierID 作为Supplier 的属性对我来说更有意义。最初的好处是您可以对供应商 ID 进行一些基本验证。当然,您现在将其表示为 int,但这可能(并且可能会)在将来发生变化。例如,您可能决定在内部将 ID 表示为字符串和整数,但在报告时,您会将其表示为字符串:XYZ1234,其中 XYZ 是供应商公司名称(字符串),1234 是唯一 ID(整数)(也许是糟糕的例子,但它仍然可能以某种方式改变)/

将供应商作为一种类型的真正优势在于,当您创建一个供应商时,您将能够使用依赖注入将供应商分配给零件部分的实例。因此,您的 Part 构造函数应如下所示:

Part(Supplier supplier)
{
   _supplier = supplier;
}

现在您的 Part 类不依赖于 Supply 类中的更改。即它不依赖于它。

注意:如果您不熟悉依赖注入,Martin Fowler 的这篇文章应该解释:

http://martinfowler.com /articles/injection.html

Supplier supplier 

Having Supplier as a type and having SupplierID as a property of Supplier would make more sense to me. The initial benefit is that you can do some basic validation on the supplier ID. Sure you are representing it as an int now but this could (and probably will) change in the future. For example, you may decide to represent the ID as a string and int internally but when reporting it you will represent it as a string: XYZ1234, where XYZ is the Supplier company name(string) and 1234 is the unique ID (int) (bad contrived example maybe, but it is still likely to change in some way)/

The real advantage of having Supplier as a type is due to the fact you will be able to use Dependancy Injection to assign the Supplier to the Part when you create an instance of Part. So your constructor for Part should look like:

Part(Supplier supplier)
{
   _supplier = supplier;
}

Now your Part class is not dependant on changes in your Supplier class. I.e. it is not dependant on it.

Note: If your not familiar with Dependancy Injection, this article from Martin Fowler should explain:

http://martinfowler.com/articles/injection.html

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