You can use the strategy template.
The idea is that you should know what strategy you are going to use when creating an instance of your class (or you can change it dynamically). Therefore, you can pass on this strategy after creating the instance (and possibly replace it later).
public interface IStartupStrategy { void Start(); } public interface IStopStrategy { void Stop(); } public class MyClass { private readonly IEnumerable<IStartupStrategy> startupStrategies; private readonly IEnumerable<IStopStrategy> stopStrategies; public MyClass(IEnumerable<IStartupStrategy> startup, IEnumerable<IStopStrategy> stop) { this.startupStrategies = startup; this.stopStrategies = stop; } public void Start() { foreach(var strategy in this.startupStrategies) { strategy.Start(); } } public void Stop() { foreach(var strategy in this.stopStrategies) { strategy.Stop(); } } }
source share