Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

July 6, 2009

Applying OOP to Simple Situations: Chinese Zodiac Signs

Filed under: C#,OOP,VB.NET @ 4:34 pm

Here is the story defining the simple use case for this application:

  1. The user picks a date between 2/19/1996 and 2/5/2019.
  2. 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:

  • Zodiac sign
  • Date range

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.

Zodiac Sign

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.

Date Range

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:

ZodiacSign

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.

ZodiacSigns

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.

DateRange

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.

Enjoy!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2020 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs