1

I'm trying to implement the Template Method Pattern in C# to handle different behaviours between all the subclasses that inherit the TemplateFilter class.

    public abstract class TemplateFilter : Operand
{

    public TemplateFilter(params string[] values) : base(setValues(values))
    {}

    public string setValues(params string[] values){

        return formatResult(values);
    }

    public abstract string formatResult(params string[] values);
}

One of the sublasses is:

public class Type3UnaryFilter : TemplateFilter
{
    public Type3UnaryFilter(string value, string field) : base(new string[] { value, field }) { }

    public override string formatResult(params string[] values)
    {
        //type{value}:field
        return "type{"+values[0]+"}:"+values[1];
    }
}

Finally, the class Operand:

public class Operand : IExpressionComponent
{
    public string FilterStatement { get; private set; }

    public Operand(string filterStatement)
    {
        this.FilterStatement = filterStatement;
    }

    public string evaluate()
    {
        return this.FilterStatement;
    }
}

My problem is that I can't call the Operand constructor from TemplateFilter, since it requires setValues to be statc. On the other hand, if I set setValues to static, I can't call the method formatResult from TemplateFilter.setValues and leave the subclasses the responsability of implementing the desired behaviour. So, what can I do in order to implement this mechanism without changing the architecture ?

davideAlbertini
  • 93
  • 2
  • 11

0 Answers0