Reputation: 1912
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; }
}
Upvotes: 5
Views: 2040
Reputation: 81247
It sounds as though what you're asking is whether there's a term for classes that require:
Thing it = new Thing(someParams);
Thing.Initialize(moreParams);
rather than
Thing it = new Thing(someParams, moreParams);
The term "two-stage initialization" is sometimes used to refer to objects where the constructor yields an object which is useless until a second method is called. While there are a few situations where such a design might make sense from an API perspective, usually such designs result from the the lack of facilities in Java or many .NET languages to control objects' construction sequence.
In many situations, construction of a useful object will require that base-class code invoke virtual methods which won't be usable until after parameters passed to the derived-class constructor have been used to set up the derived-class object. While C# allows derived-class fields whose values don't depend upon constructor parameters to be initialized before chaining to the base constructor, C# provides no clean way by which the derived class can use its constructor's parameters until after the base class constructor has given up all control over the construction process. Two-stage initialization is a somewhat ugly but workable way to deal with this.
Further, if an object must hold resources to be useful, deferring the acquisition of such resources to an Initialize
method will make it possible for client code which wraps the object in a try
or using
block to ensure that resources used by the object will get released even if the object's initialization throws an exception. Because .NET and Java go out of their way to prevent exposure of objects whose constructors throw exceptions, preventing resource leaks without using two-stage initialization is much harder than using two-stage initialization.
Upvotes: 4
Reputation: 1557
Are you looking for Dependency injection with its specialization Setter injection?
Upvotes: 2