是否可以将一个对象的实例化限制为 VB.NET 中的另一个(父)对象?

发布于 2024-08-30 20:37:46 字数 630 浏览 7 评论 0原文

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 的要求是:

  1. 只能由 Order 类实例化。

  2. 必须是公共的,以便任何其他对象都可以通过 Order 对象访问它的属性/方法。例如 Order.OrderItem(0).ProductID。

  3. 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:

  1. Can only be instantiated by the Order class.

  2. Must be public so that any other object can access it's properties/methods through the Order object. e.g. Order.OrderItem(0).ProductID.

  3. 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 技术交流群。

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

发布评论

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

评论(3

嘿嘿嘿 2024-09-06 20:37:46

您可以创建一个带有 Order 项目的 OrderItem 构造函数。

在此构造函数中,您可以将 OrderItem 添加到 Order.MyOrderItems 集合中。

这将满足您的要求。

Public Class OrderItem
    Public Sub New(ByRef myOrder As Order)
        myOrder.MyOrderItems.Add(Me)
    End Sub
End Class
  1. 您只能通过向其传递有效的 Order 对象来实例化 OrderItem
  2. 仍然是公开的,可以根据需要调用 (Order.OrderItem(0).ProductID)。
  3. OrderItem 可以传递给其他子函数和函数。

You can make a single constructor of OrderItem that takes an Order item.

In this constructor, you can add the OrderItem to the Order.MyOrderItems collection.

This will satisfy your requirements.

Public Class OrderItem
    Public Sub New(ByRef myOrder As Order)
        myOrder.MyOrderItems.Add(Me)
    End Sub
End Class
  1. You can only instantiate OrderItem by passing in a valid Order object to it.
  2. Still public and can be called as required (Order.OrderItem(0).ProductID).
  3. OrderItem can be passed to other subs and functions.
放我走吧 2024-09-06 20:37:46

执行此操作的标准方法是将 OrderItem 公开为接口,然后在 Order 类内的私有类中实现它。 OrderItem 类只能在 Order 类内部实例化,但可以通过公共 IOrderItem 接口暴露给外部。就像这样:

Public Interface IOrderItem
    ReadOnly Property ItemCode() As Integer
    ReadOnly Property NumberOfItems() As Integer
    ReadOnly Property Description() As String
End Interface

Public Class Order

    Private m_Items As List(Of IOrderItem)

    Public ReadOnly Property Items() As List(Of IOrderItem)
        Get
            Return m_Items
        End Get
    End Property

    Private Class OrderItem
        Implements IOrderItem

        Private m_Code As Integer
        Private m_NumItems As Integer
        Private m_Description As String

        Public Sub New(ByVal code As Integer, ByVal numItems As Integer, ByVal desc As String)
            m_Code = code
            m_NumItems = numItems
            m_Description = desc
        End Sub

        Public ReadOnly Property Description() As String Implements IOrderItem.Description
            Get
                Return m_Description
            End Get
        End Property

        Public ReadOnly Property ItemCode() As Integer Implements IOrderItem.ItemCode
            Get
                Return m_Code
            End Get
        End Property

        Public ReadOnly Property NumberOfItems() As Integer Implements IOrderItem.NumberOfItems
            Get
                Return m_NumItems
            End Get
        End Property
    End Class
End Class

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:

Public Interface IOrderItem
    ReadOnly Property ItemCode() As Integer
    ReadOnly Property NumberOfItems() As Integer
    ReadOnly Property Description() As String
End Interface

Public Class Order

    Private m_Items As List(Of IOrderItem)

    Public ReadOnly Property Items() As List(Of IOrderItem)
        Get
            Return m_Items
        End Get
    End Property

    Private Class OrderItem
        Implements IOrderItem

        Private m_Code As Integer
        Private m_NumItems As Integer
        Private m_Description As String

        Public Sub New(ByVal code As Integer, ByVal numItems As Integer, ByVal desc As String)
            m_Code = code
            m_NumItems = numItems
            m_Description = desc
        End Sub

        Public ReadOnly Property Description() As String Implements IOrderItem.Description
            Get
                Return m_Description
            End Get
        End Property

        Public ReadOnly Property ItemCode() As Integer Implements IOrderItem.ItemCode
            Get
                Return m_Code
            End Get
        End Property

        Public ReadOnly Property NumberOfItems() As Integer Implements IOrderItem.NumberOfItems
            Get
                Return m_NumItems
            End Get
        End Property
    End Class
End Class
陈年往事 2024-09-06 20:37:46

OrderItem 的构造函数应该是 friend (只能访问 Order) - 我说 friend 因为使用 朋友,您可以限制 OrderItem 构造函数对同一程序集中的类的可见性。 注意:此解决方案意味着 Order 和 OrderItem 位于一个库/程序集中,而您正尝试从另一程序集中使用它们 - 否则 friend 保护将不起作用。

创建 OrderItem 对象的责任应该只属于 Order

构建一个类似于 Order 类的方法:Public Function CreateOrderItem(whateverParametersYouNeed) ) 作为 OrderItem,在内部实例化一个新的 OrderItem,将其添加到 OrderItem 列表中并返回对新创建的项目的引用。

然后,您可以使用此 OrderItem 执行任何您想要的操作 - 它将始终属于 Order 的列表。

OrderItem's constructor should be friend (accesible only to Order) - I say friend because using friend you can limit the visibility of the OrderItem'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 the friend protection does not work.

The responsablity for creating OrderItem objects should belong only to Order:

Build a method like on the Order class: Public Function CreateOrderItem(whateverParametersYouNeed) as OrderItem that internally instantiates a new OrderItem, adds it to the OrderItem list and returns reference to the newly created item.

You can then do whatever you want with this OrderItem - it will always belong to the Order's list.

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