You must make the TemplateMethod settings immutable:
abstract class TemplateMethod { protected TemplateMethod(bool recursive) { Recursive = recursive; } public bool Recursive { get; private set; } protected abstract bool ItemIsWhitelisted(SomeType item); public IEnumerable<SomeType> GetItems() { } } class Implementer : TemplateMethod { protected override bool ItemIsWhitelisted(SomeType item) { Recursive = false;
UPD
Option number 2. If using ctor is difficult to pass settings, you might consider inserting an immutable parameter object. Something like this (MEF style sample):
public interface ISettings { bool Recursive { get; } } abstract class TemplateMethod { [Import] public ISettings Settings { get; private set; } protected abstract bool ItemIsWhitelisted(SomeType item); public IEnumerable<SomeType> GetItems() { } }
Of course, this means that TemplateMethod cannot change the settings either.
Option number 3. Explicit implementation of the interface (if TemplateMethod should be able to change settings):
public interface ISettingsWriter { bool Recursive { set; } } abstract class TemplateMethod : ISettingsWriter { public bool Recursive { get; private set; } bool ISettingsWriter.Recursive { set { Recursive = value; } } protected abstract bool ItemIsWhitelisted(SomeType item); } class Implementer : TemplateMethod { protected override bool ItemIsWhitelisted(SomeType item) { Recursive = true;
And of course, this means that anyone who wants to change TemplateMethod.Recursive must drop TemplateMethod in ISettingsWriter .
source share