I have a function that returns the orders for a given period. I've create a Period object which ensures that, when specifying a date range, the start date is <= to the end date. Likewise, if dealing with a month period, the start and end dates of the period should be the first and last days of the month, respectively.
My question is this:
I thought that in Object Oriented Design principles, coupling was bad. Am I introducing coupling between the Order and Period classes by having the Order class use a Period class as a parameter in one of its methods?
My guess is yes, but that there are advantages of doing so, namely, once the objects are defined, one doesn't have to perform the same parameter validation checks every time a period is passed as a parameter to different methods of the Orders class.
Besides, doesn't Microsoft constantly pass non intrinsic objects of one type to other objects?
Avoiding coupling sounds like avoiding re-use to me, which OOP was supposed to promote. This sounds like competing goals.
Can someone clarify.
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