我有一个CommonRequestBuilder
和SpecificRequestBuilder
,它们看起来像这样。
public class CommonRequestBuilder
{
protected readonly BigRequest _request;
public CommonRequestBuilder()
{
_request = new BigRequest();
}
public CommonRequestBuilder WithExtras()
{
// add extra stuff to _request
return this;
}
public BigRequest Build()
{
return _request;
}
}
public class SpecificRequestBuilder : CommonRequestBuilder
{
public SpecificRequestBuilder WithDetails()
{
// add some stuff to _request
return this;
}
public BigRequest Build()
{
return _request;
}
}
这个模式的问题在于,如果我像这样使用SpecificRequestBuilder:
_specificRequestBuilder.WithExtras().WithDetails(); // WithDetails() is not found
在上述代码中,由于我从
WithExtras()
获取基类,所以无法解析WithDetails()
。我可以重新排列方法使其工作,但是否有一种方法可以更新类,使得任何顺序都能工作?
((SpecificRequestBuilder)_specificRequestBuilder.WithExtras()).WithDetails()
- MakePeaceGreatAgain