【发布时间】:2010-10-21 19:45:25
【问题描述】:
我有一个函数可以返回给定期间的订单。我创建了一个 Period 对象,它确保在指定日期范围时,开始日期
我的问题是这样的:
我认为在面向对象设计原则中,耦合是不好的。我是否通过让 Order 类在其方法之一中使用 Period 类作为参数来引入 Order 和 Period 类之间的耦合?
我的猜测是肯定的,但是这样做有好处,即,一旦定义了对象,就不必在每次将句点作为参数传递给不同的方法时执行相同的参数验证检查。 Orders 类。
此外,Microsoft 不是经常将一种类型的非内在对象传递给其他对象吗?
对我来说,避免耦合听起来像是避免重用,而 OOP 应该促进这一点。这听起来像是相互竞争的目标。
谁能澄清一下。
Public Class Order
Public Shared Function GetOrders(ByVal customerId As Integer,
ByVal forPeriod As Period) As Orders
**'Should the param object MonthPeriod be replaced with 2 date params?
'Would I be "reducing coupling" by doing so, a good thing.**
End Function
End Class
Public Class Period
Property FromDate As Date
Property ToDate As Date
Public Sub New(ByVal fromDate As Date, ByVal toDate As Date)
If fromDate > ToDate Then
Throw New ArgumentException("fromDate must be less than Or equal toDate")
End If
_FromDate = fromDate
_ToDate = toDate
End Sub
End Class
Public Class MonthPeriod : Inherits Period
Public Sub New(ByVal fromDate As Date, ByVal toDate As Date)
MyBase.New(fromdate, toDate)
If fromDate.Date <> New Date(fromDate.Year, fromDate.Month, 1) Then
Throw New ArgumentException("fromDate must be the first day of the month")
End If
If toDate.Date <> New Date(toDate.Year, toDate.Month, Date.DaysInMonth(toDate.Year, toDate.Month)) Then
Throw New ArgumentException("fromDate must be the last day of the month")
End If
End Sub
End Class
【问题讨论】: