条件生成器方法链接流畅接口 [英] Conditional Builder Method Chaining Fluent Interface
问题描述
我想知道在流畅界面中使用方法链接 c strong>在 Builder
对象?
I was wondering what would be the best way to implement a .When
condition in a fluent interface using method chaining in a Builder
object?
例如我将如何实现 .WithSkill ()
和 .When()
方法在以下示例中:
For instance how would I implement the .WithSkill()
and .When()
methods in the following example:
var level = 5;
var ninja = NinjaBuilder
.CreateNinja()
.Named("Ninja Boy")
.AtLevel(level)
.WithShurikens(10)
.WithSkill(Skill.HideInShadows)
.When(level > 3)
.Build()
更新 - 可以找到一个示例解决方案这里。
Update - A sample solution can be found here.
推荐答案
我要做的是$ code> NinjaBuilder 将操作保留为代理列表,而不是应用它们,并且只有在 .Build
被调用时才应用它们。这将允许您使它们成为条件:
What I'd do is have NinjaBuilder
keep the operations as a list of delegates, rather than applying them, and only apply them when .Build
is called. This would allow you to make them conditional:
public class NinjaBuilder {
List<Action<Ninja>> builderActions = new List<Action<Ninja>>();
public Ninja Build() {
var ninja = new Ninja();
builderActions.ForEach(ba => ba(ninja));
return ninja;
}
public NinjaBuilder WithShurikens(int numShirukens) {
builderActions.Add(n=>n.Shirukens = numShirukens);
return this;
}
public NinjaBuilder When(Boolean condition) {
if (!condition) // If the condition is not met, remove the last action
builderActions.Remove(builderActions.Length - 1);
return this;
}
}
当然,这假定条件是恒定的建筑师创作的时间。如果你想让它不变,你可以这样做:
Of course, this assumes that the condition is constant at the time of builder creation. If you want to make it non-constant, you could do something like this instead:
public NinjaBuilder When(Func<Boolean> condition) {
var oldAction = builderActions[builderActions.Length - 1];
builderActions[builderActions.Length - 1] = n => condition() ? oldAction(n) : n;
return this;
}
如果你想要当
有些更多的编译器检查,你可以使builderActions保护,并做这样的事情:
If you want When
be somewhat more compiler checked, you can make builderActions protected and do something like this:
public class ConditionalNinjaBuilder : NinjaBuilder {
public ConditionalNinjaBuilder(NinjaBuilder wrappedBuilder) {
// Since someone might call .WithShirukens on the wrapping
// builder directly, we should make sure that our actions
// list is the same instance as the one in our wrapped builder
builderActions = wrappedBuilder.builderActions;
}
public ConditionalNinjaBuilder When(Func<Boolean> condition) {
var oldAction = builderActions[builderActions.Length - 1];
builderActions[builderActions.Length - 1] = n => condition() ? oldAction(n) : n;
return this;
}
}
并且原始操作返回一个ConditionalNinjaBuilder: p>
and have the original operations return a ConditionalNinjaBuilder:
public ConditionalNinjaBuilder WithShurikens(int numShirukens) {
builderActions.Add(n=>n.Shirukens = numShirukens);
return new ConditionalNinjaBuilder(this);
}
这样你只能调用 code>首先调用另一个方法。这还具有潜在允许嵌套/复合条件的附加优点/复杂性。 Yikes。
That way you can only call .When
after first calling another method. This has the additional advantage/complication of potentially allowing for nested/compounded conditionals, too. Yikes.
这篇关于条件生成器方法链接流畅接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!