Tag Archives: softwareengineering

Effective .NET via Effective Java: Typesafe Enums

December 13, 2010 | Jason Kozemczak

A few weeks ago I bore through Joshua Bloch’s Effective Java.  I’d heard a number of good things about the text, and even though I code professionally in .NET these days, good OO practice is good OO practice regardless of the language.

Bloch’s book is laid out in the same fashion as the classic Effective C++ (Scott Meyers); Bloch lays out 57 suggestions to writing “better” Java code.  One of the ones that gave me an “A ha” moment was Item #21, which presents the “typesafe enum” pattern.  Bloch’s examples are of course in Java, but the pattern can be utilized in .NET (and probably a number of other languages, though I’ll focus on .NET in this post).

Bloch proposes that more often than not, Enums can (and should) be replaced by class behavior.  I’ll present an example to illustrate the advantages to this methodology.

Let’s assume we’re implementing a card game in .NET.  We’d quickly find the need to enumerate the various suits of cards.  It’d probably look something like this

This would probably work well enough, but this doesn’t really get us very far.  Enums can’t implement methods, inherit interfaces, etc.  They’re a little bit more than named integer constants  We can pretty easily get this sort of functionality plus a quite a bit more with just a bit more work.  Now, consider the following class:

What we do by making the constructor private is limit the instantiated instances to only the 4 public facing static instances, which each represent the four suits.  We can now access those instances in a similar fashion to how we access Enum values: Suit.CLUBS, Suit.DIAMONDS, etc.

Now, the above example is somewhat trivial, but where the power lies is that we are now working with dyed-in-the wool classes, so we can add all the instance methods and properties we can dream up.  One of the great bonuses is that since we know there will only be four instances of this class, object equality “just works” since we know that all references to instances of the Suit class will relate back to one of the public-facing four.

We can go even further: since our Suits are now just instances of a class, we can make the Suit class implement any interface we choose, and we can gain the benefits to that implementation (IComparable comes to mind).

One of the other issues Bloch delves in on is the issues around Serialization.  If you are de-serializing instances of the Suit class, this will introduce additional instances of the Suit class; though I’m not going to dive into the details, it’s possible to also overcome issues revolving around that.

I can imagine a number of applications where a typesafe enum class makes more sense than the traditional Enum.  That said, each situation is different, and certainly there are situations where this pattern doesn’t offer any sizable advantages.  Can you think of a time when you used an Enum where the typesafe enum pattern made more sense?  Maybe I haven’t done enough to convince you of the power behind this pattern?  Let me know in the comments!  I might follow up with a more “real world” example where this pattern can be leveraged!

There are numerous conditions such as cancer which have no cure. It isn’t hard for immigrants to buy medicines online. How it is possible? You can purchase medicament to treat chronic treatment of the symptoms of osteoarthritis or muscle rigidity. There are different other medicaments. What about levitra cost and how much does levitra cost? What do you already know about http://cialis-vs-viagra.biz/why-order-viagra-online-the-perks-of-internet-shopping-for-meds.html? (Read more cost of levitra). Currently ten percent of men aged 40 to 70 were have trouble keeping an erection during sex. It affects men in all parts of the world. Generally, having trouble getting an erection can be knotty. All kinds of drugs, from those that are elaborate “all natural” to those that are chemically produced in a laboratory, carry some kind of aftereffects. Along with them useful effects, most drugs, notwithstanding, can cause dangerous side effects although usually not everyone experiences them. Online physician services are the only safe variation if you are going to purchase drugs, like Levitra, online.