There is a base class, others are inherited from it, these other classes are implemented in themselves new entities, how to avoid repeating the same code, with partial repetition of some entities in inherited classes? It is important that all new entities within inherited classes take on different incoming values.
// базовый класс abstract class BaseStrategy { // переменная для примера, она будет использована в наследуемом классе protected int VariableForSignal = 77; // переменная для примера, она будет использована в наследуемом классе protected int VariableForOpen = 5; // другой manager AnotherManager AnotherManager = new AnotherManager(); // основная функция в которую поступают данные public void GetDecimalUpdate(decimal IncomeValue) { // отпрашиваем первый фильтр if (!SignalFilterAnswer(IncomeValue)) return; // опрашиваем второй фильтр if (!OpenFilterAnswer(IncomeValue)) return; // опрашиваем третий фильтр if (!AnotherManager.ReturnAnswer()) return; } // реализацию ответа фильтра оставляем наследуемым классам protected abstract bool SignalFilterAnswer(decimal IncomeValue); // реализацию ответа фильтра оставляем наследуемым классам protected abstract bool OpenFilterAnswer(decimal IncomeValue); } // наследуем базовый класс class StupidPaperStrDiffStrategy : BaseStrategy { // signal filter StrDiff StrDiff = new StrDiff(); // open filter StupidPaper StupidPaper = new StupidPaper(); // реализуем метод, который выполняет работу "signal filter" protected override bool SignalFilterAnswer(decimal IncomeValue) { // этот IF для того чтобы показать, что используются protected переменные базового класса if (VariableForSignal < IncomeValue) return StrDiff.ReturnAnswer(true, new int[] { 1, 2 }); else return StrDiff.ReturnAnswer(false, new int[] { 2, 3 }); } // реализуем метод, который выполняет работу "open filter" protected override bool OpenFilterAnswer(decimal IncomeValue) { // этот IF для того чтобы показать, что используются protected переменные базового класса if (VariableForOpen > IncomeValue) return StupidPaper.ReturnAnswer(); else return StupidPaper.ReturnAnswer(); } } The problem is that when I create another inherited class, and I use for example the implementation of StrDiff , but together StupidPaper will have a TimeFrame implementation of working with StrDiff will be in two classes at once, how to avoid it? Example:
// наследуем базовый класс class TimeFrameStrDiffStrategy : BaseStrategy { // signal filter StrDiff StrDiff = new StrDiff(); // open filter TimeFrame TimeFrame = new TimeFrame(); // реализуем метод, который выполняет работу "signal filter" protected override bool SignalFilterAnswer(decimal IncomeValue) { ////////////////////////////////////////// // Проблема находиться тут, этот код уже был написан в классе StupidPaperStrDiffStrategy и повторяется в этом классе ///////////////////////////////////////// if (VariableForSignal < IncomeValue) return StrDiff.ReturnAnswer(true, new int[] { 1, 2 }); else return StrDiff.ReturnAnswer(false, new int[] { 2, 3 }); } // реализуем метод, который выполняет работу "open filter" protected override bool OpenFilterAnswer(decimal IncomeValue) { // этот IF для того чтобы показать, что используются protected переменные базового класса if (VariableForOpen > IncomeValue) return TimeFrame.ReturnAnswer(new byte[] { 144, 4 }); else return TimeFrame.ReturnAnswer(new byte[] { 12, 4 }); } }