我有一个函数可以返回给定期间的订单。我创建了一个Period对象,可以确保在指定日期范围时,开始日期为< =到结束日期。同样,如果处理一个月的时间段,则该时间段的开始日期和结束日期应分别为该月的第一天和最后一天。OOP/OOD耦合问题
我的问题是这样的:
我认为,在面向对象的设计原则,联轴器是坏的。我是否通过让Order类在其方法之一中使用Period类作为参数来引入Order和Period类之间的耦合?
我的猜测是肯定的,但这样做的好处是,即一旦定义了对象,就不必每次将句点作为参数传递给不同的方法时都执行相同的参数验证检查Orders类。
此外,Microsoft是否不经常将一种类型的非内在对象传递给其他对象?
避免耦合听起来像避免重复使用我,这是面向对象应该促进。这听起来像是相互竞争的目标。
有人可以澄清。
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
优秀的答案。谢谢 – ChadD 2010-10-22 02:08:47
Definetly同意...优秀的答案。谢谢(我多次阅读这些内容,但是当你看到如此精细的评论时(为什么/何时/细节),那么在你的代码中应该选择哪种方式。谢谢史蒂夫。 – ramnz 2012-11-20 17:03:55