父对象创建子对象 [英] Parent creating child object
问题描述
我正在编写一个具有 Reactor 和 Zone 对象的应用程序.每个 Reactor 都有可变数量的区域,并将它们存储在名为 _zones 的私有字段中.
I'm writing an application that has Reactor and Zone objects. Each Reactor has a variable number of zones, and stores them in a private field called _zones.
反过来,每个 Zone 必须知道它属于哪个 Reactor,因此它在私有字段中存储了一个指向它的 reactor 的指针.我通过将一个 reactor 对象传递给 Zone 构造函数来分配这个字段.
In turn, each Zone must know to which Reactor does it belong, so it stores a pointer to its reactor in a private field. I assign this field by passing a reactor object into the Zone constructor.
这是示例代码:
Public Class Reactor
Private _zones As New List(Of Zone)
End Class
Public Class Zone
Private _reactor As Reactor
Public Sub New(ByVal reactor As Reactor)
_reactor = reactor
End Sub
End Class
我的问题是:由于每个 Zone 都需要将一个 reactor 传递给其构造函数,您认为这两种方法中哪一种更好:
My question is: since each Zone needs to have a reactor passed to its constructor, which of this two approaches do you consider to be better:
仅使用 Reactor 中的方法创建区域.
Create Zones only with a method in Reactor.
Public Class Reactor
Private _zones As New List(Of Zone)
Public Sub CreateZone()
_zones.Add(New Zone(Me))
End Sub
End Class
创建区域并将它们添加到反应器
Create zones and them add them to the reactor
Public Class Reactor
Private _zones As New List(Of Zone)
Public Sub AddZone(ByVal z As Zone)
_zones.Add(z)
End Sub
End Class
我认为所选择的选项取决于具体情况,但我想知道这些选项中的一个是否被普遍认为是最佳实践.此外,任何类似案例的经验都会非常有用.
I suppose that the chosen option depends on the concrete case, but I would like to know if one of these options is considered generally as a best practice. Also, any experience about similar cases would be very useful.
谢谢!
推荐答案
我会说第一个选项是首选.第二种方法的问题是可以创建一个 Zone
,在它的构造函数中给它 reactorA
,然后将它添加到 reactorB
>.或者,您可以将相同的 Zone
添加到两个不同的 Reactor
对象.您当然可以为此添加错误检查并在不匹配的情况下抛出异常,但是只要设计需要父/子关系,您就永远不可能处于需要创建的位置一个 Zone
,但你无权访问它的 Reactor
.只要您在需要创建 Zone
时可能始终可以访问 Reactor
,那么没有理由不通过将创建"Reactor
类中的方法.
I would say that the first option is preferred. The problem with the second approach is that it would be possible to create a Zone
, giving it reactorA
in its constructor, but then add it to reactorB
. Or, you could add the same Zone
to two different Reactor
objects. You could of course add error-checking for that and throw an exception in the case of a mismatch, but as long as the design requires the parent/child relationship, you'll never be likely to be in a position where you need to create a Zone
, but you don't have access to its Reactor
. As long as you're likely to always have access to the Reactor
when you need to create the Zone
, then there's no reason not to encapsulate it all by putting the "create" method in the Reactor
class.
例如,我创建了一个网格对象,它定义了要打印到页面或屏幕的网格表.它包含一个行列表,每行包含一个单元格列表.行需要对其网格的引用,而单元格需要对其行的引用.所以,我只是以一种方式实现它,您告诉网格您要添加一行并为其指定大小,如下所示:
For instance, I have created a grid object, which defines a grid table to be printed to a page or to the screen. It contains a list of rows and the rows each contain a list of cells. The rows need a reference back to their grid and the cells need a reference back to their row. So, I simply implemented it in a way where you tell the grid that you want to add a row and give it the size, like this:
myGrid.Rows.Add(100)
Add
方法返回 Row 对象,因此您可以像这样轻松获取它:
And the Add
method returns the Row object, so you can get it easily like this:
Dim row As GridRow = myGrid.Rows.Add(100)
row.PenWidthTop = 1
这种设计非常适合这种特殊情况.由于我不太可能需要在我还没有对父网格的引用的地方创建一行,因此它非常好用且易于使用.
This design works very well for that particular situation. Since it's very unlikely that I'll ever need to create a row in a place where I won't already have a reference to the parent grid, it makes it nice and easy to work with.
如果您确实遇到过需要能够独立创建 Zone
对象,然后将它们添加到 Reactor
的情况,您可以随时添加作为次要选项.第一种方法不会阻止您稍后添加第二种方法.例如,你可以让 Zone
对象允许自己在没有父 Reactor
的情况下被创建,然后当它们被添加到 Reactor
时code>,Reactor
可以在Zone
上设置一个属性来告诉它它的父Reactor
是谁.如果您在之前已经设置了它之后尝试更改它,您可以让 Zone
类上的 ParentReactor
属性抛出异常,如下所示:
If you ever do run into a situation where you need to be able to create Zone
objects independently, and then add them to the Reactor
later, you could always add that as a secondary option. There's nothing about the first method which precludes you from adding the second method at a later time. For instance, you could make it so that Zone
objects allow themselves to be creates without a parent Reactor
, and then, when they are added to the Reactor
, the Reactor
can set a property on the Zone
to tell it who it's parent Reactor
is. You could have the ParentReactor
property on the Zone
class throw an exception if you try to change it after it's been already set before, like this:
Public Class Zone
Public Sub New()
End Sub
Public Sub New(parentReactor As Reactor)
_parentReactor = parentReactor
End Sub
Private _parentReactor As Reactor
Public Property ParentReactor As Reactor
Get
Return _parentReactor
End Get
Set(value As Reactor)
If _parentReactor IsNot Nothing Then
Throw New Exception("This zone has already been added to a Reactor")
End If
_parentReactor = value
End Set
End Property
' ...
End Class
这篇关于父对象创建子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!