Dragging enums into the OO world

   1: public enum Weekday {
   2:     Monday,
   3:     Tuesday,
   4:     Wednesday,
   5:     Thursday,
   6:     Friday,
   7:     Saturday,
   8:     Sunday
   9: };

 

What's wrong with this code?

This looks like C# but actually you could already do this in C in the 1970's. In those days the ability to define your own types was a novel idea but in today's object-oriented languages we expect a bit more, like extensibility or the possibility to inherit from types.

What if we want to extend our Weekday-type with a method to check for weekends? Or a 'Next' and 'Previous' method? Usually these will end up as static methods in some Utility class. Smelly! I want to be able to do this:

   1: if( Weekday.Saturday.IsWeekend() ) {
   2:    ...
   3: }

Let's try again. This time with class :-)

   1: public class Weekday {
   2:     public static Weekday Monday = new Weekday();
   3:     public static Weekday Tuesday = new Weekday();
   4:     public static Weekday Wednesday = new Weekday();
   5:     public static Weekday Thursday = new Weekday();
   6:     public static Weekday Friday = new Weekday();
   7:     public static Weekday Saturday = new Weekday();
   8:     public static Weekday Sunday = new Weekday();
   9: }

Now why exactly is this better? It looks like a lot more work.

Maybe because you can extend it like this?

   1: public class Weekday {
   2:     public static Weekday Monday = new Weekday();
   3:     public static Weekday Tuesday = new Weekday();
   4:     public static Weekday Wednesday = new Weekday();
   5:     public static Weekday Thursday = new Weekday();
   6:     public static Weekday Friday = new Weekday();
   7:     public static Weekday Saturday = new Weekday();
   8:     public static Weekday Sunday = new Weekday();
   9:  
  10:     public bool IsWeekend() {
  11:         // ...
  12:     }
  13:  
  14:     public Weekday Next() {
  15:         // ...
  16:     }
  17:  
  18:     public Weekday Prev() {
  19:         // ...
  20:     }
  21: }

Looks familiar? Take a look at the System.Drawing.Color class.

Print | posted on Saturday, December 15, 2007 3:59 PM

Feedback

# re: Dragging enums into the OO world

Left by Andries van Waas at 12/16/2007 11:57 PM
Gravatar In java you can add both data and behaviour to your enums, see:

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

Would be a nice addition to c# in .net 4.0 (c# 3.5???)

# re: Dragging enums into the OO world

Left by Mendelt Siebenga at 12/18/2007 1:23 PM
Gravatar Hey Andries,

I found the 'trick' I posted here in a Java patterns book. Before v5 Java didn't support enums and this was a common way to fake them. So it makes sense that they implemented enums this way with some nice syntactic sugar to save you some typing (although I'm no Java expert so I'm not sure it works this way)

# re: Dragging enums into the OO world

Left by Michalis Sarigiannidis at 1/6/2008 5:53 PM
Gravatar I think you forgot to make your fields readonly, right? :-)

# re: Dragging enums into the OO world

Left by Mendelt Siebenga at 1/9/2008 10:05 PM
Gravatar Hi Michalis,

You're right. The static fields should be readonly too. Oops!

Your comment:





 
Please add 4 and 3 and type the answer here:

Copyright © Mendelt Siebenga

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski