假设我有 A 类:
public class A
{
private B _b;
public class A(B b)
{
Assert.That(b != null);
_b = b;
}
}
对象 b 需要一些复杂的初始化,例如:
b.Prop1 = ...
b.Prop2 = ...
b.Prop3 = ...
int answerToSomeComplexFormula = PerformComplexFormula();
b.Prop4 = answerToSomeCopmlexFormula
etc...
我不想在构造函数中执行这个初始化。是否有一些描述返回具有复杂初始化的对象的模式的名称?就像是:
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)
谢谢!