Is there an in-depth article explaining it? I want to be aware of available solutions and to avoid re-inventing patterns.
The idea is that an instance is useful even without being completely initialized and the initialization dependencies are not known at construction time. So the instance construction is a two step process: first create via a normal constructor, then initialize via a public method at some (possibly much later) stage.
Here is a sample:
public static void Main()
{
var mutation = new Mutation("Specimen 1");
//Do something with mutation - log, report etc.
if (!mutation.IsInitialized)
{
var first = new Creature("Cat", new List<string> {"Paws", "Whiskers"});
var second = new Creature("Crow", new List<string> { "Wings", "Beak" });
mutation.Initialize(first, second);
}
Console.WriteLine(mutation.CommonTraits.Aggregate((p,n) => p + ", " + n));
Console.ReadKey();
}
public class Mutation
{
public Mutation(string name)
{
Name = name;
}
public string Name { get; set; }
public Creature First { get; set; }
public Creature Second { get; set; }
public List<string> CommonTraits { get; set; }
public bool IsInitialized { get; private set; }
public void Initialize(Creature first, Creature second)
{
First = first;
Second = second;
CommonTraits = new List<string>();
CommonTraits.AddRange(first.Traits); //TODO: select randomly.
CommonTraits.AddRange(second.Traits); //TODO: select randomly.
IsInitialized = true;
}
}
public class Creature
{
public Creature(string name, List<string> traits)
{
Name = name;
Traits = traits;
}
public string Name { get; private set; }
public List<string> Traits { get; private set; }
}