I find it a nuisance to define so many methods merely in order to override Equals.
Therefore I sometimes define and invoke a method named Matches instead.
Given that you only have one instance of each gesture, do you even need to define it? The default implementation of object.Equals is to call object.ReferenceEquals.
Your Seal functionality is ugly, for several reasons. Two alternatives:
- Define AddDefeats as private; create a static constructor of the Gesture class, and construct the list of Gesture instance in the static constructor (which can invoke the private method)
Define AddDefeats as private; create a static constructor of the Gesture class, and construct the list of Gesture instance in the static constructor (which can invoke the private method)
class Gesture { internal static IList<Gesture> AllGestures; static Gesture { var spock = new Gesture("Spock"); var lizard = new Gesture("Lizard"); var paper = new Gesture("Paper"); var rock = new Gesture("Rock"); var scissors = new Gesture("Scissors"); spock.AddDefeats(new[] { scissors, rock }); lizard.AddDefeats(new[] { paper, spock }); paper.AddDefeats(new[] { rock, spock }); rock.AddDefeats(new[] { lizard, scissors }); scissors.AddDefeats(new[] { paper, lizard }); AllGestures = new List<Gesture> {rock, paper, scissors, lizard, spock}; } - Remove the AddDefeats method, and pass the array of defeats as a parameter to the Gesture constructor (so that a Gesture instance is fully-constructed immediately)
Remove the AddDefeats method, and pass the array of defeats as a parameter to the Gesture constructor (so that a Gesture instance is fully-constructed immediately)
// defeats expressed using strings (gesture names) instead of Gesture instances // because we need to refer to gestures which haven't been constructed yet public Gesture(string name, IEnumerable<string> defeats) { if (name == null) throw new ArgumentNullException("name"); Name = name; foreach (var gesture in defeats) { _defeats.Add(gesture); } }