是否可以将一个对象的实例化限制为 VB.NET 中的另一个(父)对象?
VB 2008 .NET 3.5
假设我们有两个类,Order 和 OrderItem,它们代表某种类型的在线订购系统。 OrderItem 表示订单中的单个行项目。一个Order可以包含多个OrderItem,以List(of OrderItem)的形式。
Public Class Order
Public Property MyOrderItems() as List(of OrderItem)
End Property
End Class
没有订单就不应存在 OrderItem,这是有道理的。换句话说,OrderItem 类不应该能够自行实例化,它应该依赖于 Order 类来包含它并实例化它。但是,OrderItem 的范围应该是公共的,以便其他对象可以访问它的属性。因此,OrderItem 的要求是:
只能由 Order 类实例化。
必须是公共的,以便任何其他对象都可以通过 Order 对象访问它的属性/方法。例如 Order.OrderItem(0).ProductID。
OrderItem 应该能够传递给对其进行操作的其他子程序/函数。
我怎样才能实现这些目标?有更好的方法吗?
VB 2008 .NET 3.5
Suppose we have two classes, Order and OrderItem, that represent some type of online ordering system. OrderItem represents a single line item in an Order. One Order can contain multiple OrderItems, in the form of a List(of OrderItem).
Public Class Order
Public Property MyOrderItems() as List(of OrderItem)
End Property
End Class
It makes sense that an OrderItem should not exist without an Order. In other words, an OrderItem class should not be able to be instantiated on its own, it should be dependent on an Order class to contain it and instantiate it. However, the OrderItem should be public in scope so that it's properties are accessible to other objects. So, the requirements for OrderItem are:
Can only be instantiated by the Order class.
Must be public so that any other object can access it's properties/methods through the Order object. e.g. Order.OrderItem(0).ProductID.
OrderItem should be able to be passed to other subs/functions that will operate on it.
How can I achieve these goals? Is there a better approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建一个带有
Order
项目的OrderItem
构造函数。在此构造函数中,您可以将
OrderItem
添加到Order.MyOrderItems
集合中。这将满足您的要求。
Order
对象来实例化OrderItem
。Order.OrderItem(0).ProductID
)。You can make a single constructor of
OrderItem
that takes anOrder
item.In this constructor, you can add the
OrderItem
to theOrder.MyOrderItems
collection.This will satisfy your requirements.
OrderItem
by passing in a validOrder
object to it.Order.OrderItem(0).ProductID
).执行此操作的标准方法是将 OrderItem 公开为接口,然后在 Order 类内的私有类中实现它。 OrderItem 类只能在 Order 类内部实例化,但可以通过公共 IOrderItem 接口暴露给外部。就像这样:
The standard way to do this is to expose the OrderItem as an interface and then implement it in a private class inside the Order class. The OrderItem class can only be instantiated inside the Order class but it can be exposed to the outside through the public IOrderItem interface. Like so:
OrderItem
的构造函数应该是friend
(只能访问Order
) - 我说friend
因为使用朋友
,您可以限制OrderItem
构造函数对同一程序集中的类的可见性。 注意:此解决方案意味着 Order 和 OrderItem 位于一个库/程序集中,而您正尝试从另一程序集中使用它们 - 否则friend
保护将不起作用。创建
OrderItem
对象的责任应该只属于Order
:构建一个类似于
Order
类的方法:Public Function CreateOrderItem(whateverParametersYouNeed) ) 作为 OrderItem
,在内部实例化一个新的OrderItem
,将其添加到OrderItem
列表中并返回对新创建的项目的引用。然后,您可以使用此
OrderItem
执行任何您想要的操作 - 它将始终属于Order
的列表。OrderItem
's constructor should befriend
(accesible only toOrder
) - I sayfriend
because usingfriend
you can limit the visibility of theOrderItem
's constructor to classes in the same assembly. Note: this solution implies that Order and OrderItem are in one library/assembly and you are trying to use them from another assembly - otherwise thefriend
protection does not work.The responsablity for creating
OrderItem
objects should belong only toOrder
:Build a method like on the
Order
class:Public Function CreateOrderItem(whateverParametersYouNeed) as OrderItem
that internally instantiates a newOrderItem
, adds it to theOrderItem
list and returns reference to the newly created item.You can then do whatever you want with this
OrderItem
- it will always belong to theOrder
's list.