Applying OOP to Simple Situations: Chinese Zodiac Signs
Mon, Jul 6 2009 14:34
Here is the story defining the simple use case for this application:
- The user picks a date between 2/19/1996 and 2/5/2019.
- The system displays the appropriate Chinese zodiac sign (Monkey, Dog, Rat, etc)
Seems simple enough. So how to implement this …
Defining the Classes
The first step in using OOP with a simple situation is the same as with any application … define the "nouns”.
These are the first nouns I came up with:
The next step is to think through each of these nouns and determine which make sense as classes for building the code to support this feature.
This feature needs to work with a zodiac sign, so a ZodiacSign class makes sense.
In addition, this feature needs to retain the set of zodiac signs. So a ZodiacSigns (plural) class is also needed to track the list of ZodiacSign instances.
Each zodiac sign is associated with a date range, so a DateRange class makes sense to track the dates.
Defining the Properties and Methods
The next step is to define what data that each class retains (called properties in OOP) and what functionality that the class provides (called methods in OOP).
After reviewing the nouns, three classes were defined:
This class provides the definition of a single Chinese zodiac sign. The properties for this class include:
- Name: Name of the sign such as “dog” or “monkey”.
- DateRanges: Set of date ranges associated with the sign.
This class manages the list of all zodiac signs. To leverage the .NET Framework List features, this class can inherit from the built in generic List class.
This class has two methods:
- InitializeCollection: Builds the list with the set of zodiac signs and date ranges.
- FindSign: Given a date, finds the sign.
This class has two simple properties:
- Start Date: First date of the date range.
- End Date: End date of the date range.
Using the Classes
The user interface portion of the application creates an instance of the ZodiacSigns class and calls the FindSign method as needed.