Reputation: 6231
Say I have class A:
public class A
{
private B _b;
public class A(B b)
{
Assert.That(b != null);
_b = b;
}
}
And object b needs some complex initialization, like:
b.Prop1 = ...
b.Prop2 = ...
b.Prop3 = ...
int answerToSomeComplexFormula = PerformComplexFormula();
b.Prop4 = answerToSomeCopmlexFormula
etc...
I do not want to perform this initialization in the constructor. Is there a name for some pattern that describes returning an object that has complex initialization? Something like:
public class BInitializer
{
public B Create()
{
B b = new B();
// set up properties
return b;
}
}
BInitializer initializer = new BInitializer();
B b = initializer.Create();
A a = new A(b)
Thanks!
Upvotes: 1
Views: 8468
Reputation: 1292
Your solution with BInitializer is very good and it is called Factory Method design pattern.
Here you can find some common creational design patterns: https://sourcemaking.com/design_patterns/creational_patterns
Upvotes: 4