This one was pretty much TheoY’s.  I coded it up, which probably means I corrupted his idea.  So, give him credit for the good parts and I’ll take balme for the bad parts. 

 

Pretty much the same approach was taken in C++ by Johny in his comment.

 

      abstract class E

      {

            private E() { }

 

            public abstract void OperationF();

 

            public class _a : E { public override void OperationF() { } }

            public static readonly _a a = new _a();

 

            public class _b : E { public override void OperationF() { } }

            public static readonly _b b = new _b();

 

            public class _c : E { public override void OperationF() { } }

            public static readonly _c c = new _c();

 

      }

 

      class Program

      {

            void F(E e)

            {

                  E e2 = E.a;

 

                  e.OperationF();

 

                  if (e == E.a)

                  {

                        return;

                  }

            }

      }

 

So, by making them into singletons, we lose the ability to switch() on the value.  But that’s actually a good thing – we’re doing one of the “ReplaceTypeCode” Refactorings from Fowler.

 

Are there any other interesting solutions, or is this pretty much it?