說我有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)
謝謝!
工廠模式 – clcto