Boden
Boden

Reputation: 4167

OOD: multiple objects representing computed values

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.

Upvotes: 1

Views: 112

Answers (2)

AnthonyWJones
AnthonyWJones

Reputation: 189487

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

Upvotes: 3

boutta
boutta

Reputation: 24629

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.

Upvotes: 1

Related Questions