mardi 2 juin 2020

Inheritance Fluent Builder with reference back to parent?

Not sure I am doing this correctly but essentially I have an object Foo which has heaps of configurations. I am using the Fluent Builder pattern to set the params as it makes it easier to mix & match. Once the parameters are set I wish to essentially "run" the analysis...

The complication is with the Bar object which is a slightly different calculation method uses a combined set of the Foo params + Bar params.

My initial thoughts is to have an abstract calculation class BaseCalc which contains common calculations and set either FooCalc or BarCalc which contains the custom calculations depending if AddBar is called.

  analysis = new FooBuilder()
                .SetData(data)
                .SetParam1("Foo")
                .AddBar(builder => builder                        
                    .SetParam2("Bar")
                    )         
            .Build();

public class FooBuilder: IFooBuilder
{
  private readonly Foo foo;
  public IFooBuilder SetData(List<int> data)
  {
    foo.source = data;
    return this;
  }

  public IFooBuilder SetParam1(string param)
  {
    foo.param1 = param;
    return this;
  }

  public IFooBuilder(AddBar(Action<IBarBuilder> configure)
  {
    var builder = new BarBuilder();
    configure(builder);
    foo.Bar = builder.Build();
    return this;
  }
}

As an example if I called analysis.Run() in the example above it would run BarCalc.Run() using both Foo and Bar

Aucun commentaire:

Enregistrer un commentaire