tags:

views:

61

answers:

2

Sorry for the bad question title.

Let's say that I have DateRange class that basically just consists of a start date and and end date.

My question is: How might I represent computed date ranges such as "This Week", "The Past Two Weeks", "Last Month", and "This Quarter", such that multiple clients can use these computed date ranges in a consistent way? That is, I'm going to have multiple objects of multiple classes that all need to know what "This Week" is.

I could build them into DateRange, but that doesn't seem like a good solution because more might be added or some may become obsolete over time....in fact this will happen most certainly. I could make DateRange abstract and extend it, but then I'm going to have a bunch of tiny classes. I could create some kind of static helper class that computes these dates. How might you approach this problem?

dateRange = new DateRange(RangeEnum.THISWEEK));

or

dateRange = new ThisWeek();

or

dateRange = DateHelper.getThisWeek();

or

?

To throw an additional wrench into this thing, let's say that I also need to represent a range like "the work week containing the date May 1, 2008". Upon stating that, I'm leaning towards the helper class...it just feels awkward having a big lump of goofy methods.

+1  A: 

I would go the way with the helper class.

DateRange dr = Helper.getThisWeek();

You're right, you'll get a bunch a methods, but if you would subclass something you'll get a bunch of classes. This way you know for sure where to look.

boutta
+3  A: 

Why create a helper class. The common idiom used in the .NET framework would be to add Static properties to the existing class.

class DateRange
{
     DateRange ourCurrentWeek = null;

     public static DateRange ThisWeek
     {
          get
          {
              //create ourCurrentWeek if not yet assigned.
              //test current week; is it still correct or does it need updating
              return ourCurrentWeek;
          }
     }
 }

Usage is simply:-

DateRange.ThisWeek
AnthonyWJones
That makes sense. Essentially the "helper class" idea is just integrated into the DateRange class. It feels on one hand like this forces DateRange to know too much about business requirements that it shouldn't have to...on the other hand it's simple and not a big deal.
Boden
@Boden: I guess that depends on whether you consider the DateRange class to a business object or a system infrastructure object. In financial systems what constitutes ThisWeek, Month or Quarter may vary so their definition would have to be in a business object
AnthonyWJones