How to represent different entities that have identical behavior?
<p>I have several different entities in my domain model (animal species, let's say), which have a few properties each. The entities are readonly (they do not change state during the application lifetime) and they have identical behavior (the differ only by the values of properties).</p>

<p><strong>How to implement such entities in code?</strong></p>

<h2>Unsuccessful attempts:</h2>

<h3>Enums</h3>

<p>I tried an enum like this:</p>

<pre><code>enum Animals {
    Frog,
    Duck,
    Otter,
    Fish  
}
</code></pre>

<p>And other pieces of code would switch on the enum. However, this leads to ugly switching code, scattering the logic around and <a href="http://stackoverflow.com/questions/58743/databinding-an-enum-property-to-a-combobox-in-wpf">problems with comboboxes</a>. There's no pretty way to list all possible Animals. Serialization works great though.</p>

<h2>Subclasses</h2>

<p>I also thought about where each animal type is a subclass of a common base abstract class. The implementation of Swim() <strong>is the same for all Animals</strong>, though, so it makes little sense and serializability is a big issue now. Since we represent an animal <em>type</em> (species, if you will), there should be one instance of the subclass per application, which is hard and weird to maintain when we use serialization.</p>

<pre><code>public abstract class AnimalBase {
    string Name { get; set; } // user-readable
    double Weight { get; set; }
    Habitat Habitat { get; set; }
    public void Swim(); { /* swim implementation; the same for all animals but depends                  uses the value of Weight */ }
}

public class Otter: AnimalBase{
    public Otter() {
        Name = "Otter";
        Weight = 10;
        Habitat = "North America";
    }
}

// ... and so on
</code></pre>

<p>Just plain awful.</p>

<h2>Static fields</h2>

<p><a href="http://kijanawoodard.com/why-enums-suck/">This blog post</a> gave me and idea for a solution where each option is a statically defined field inside the type, like this:</p>

<pre><code>public class Animal {
   public static readonly Animal Otter = 
       new Animal 
       { Name="Otter", Weight = 10, Habitat = "North America"}
   // the rest of the animals...

   public string Name { get; set; } // user-readable
   public double Weight { get; set; }
   public Habitat Habitat { get; set; }

   public void Swim();
</code></pre>

<p>}</p>

<p>That would be great: you can use it like enums (<code>AnimalType = Animal.Otter</code>), you can easily add a static list of all defined animals, you have a sensible place where to implement <code>Swim()</code>. Immutability can be achieved by making property setters protected. There is a major problem, though: it breaks serializability. A serialized Animal would have to save all its properties and upon deserialization it would create a new instance of Animal, which is something I'd like to avoid.</p>

<p>Is there an easy way to make the third attempt work? Any more suggestions for implementing such a model? </p>
