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 }); } } 

    2 answers 2

    We need to make a second abstract class, which is inherited from BaseStrategy and implements the SignalFilterAnswer method, but does not implement the OpenFilterAnswer method. The StupidPaperStrDiffStrategy and TimeFrameStrDiffStrategy inherit from it and implement the OpenFilterAnswer method.

    • 'MiddleClass' doesn’t implement inherited abstract member 'BaseStrategy.OpenFilterAnswer (decimal)' <br> How can I not implement it? - K.Oleg
    • one
      to declare a class as abstract - Artem Nikolaevich
    • thank you very much. Tell me this is normal practice? Or should it be somehow different to implement this task? I mean, what answer did you give based on my question, but are there other ways to implement it? - K.Oleg
    • one
      Yes, normal practice. - Artem Nikolaevich
    • There are other ways, it all depends on how many classes will be inherited and how they will be used. If only these two, then you can make it easier to remove the second abstract class and implement SignalFilterAnswer right inside BaseStrategy. - Artem Nikolaevich

    In this case, as I see it, you can use the SignalFilterAnswer method to make virtial add the default implementation to it directly in the BaseStrategy class and remove the implementation of this method from the child classes:

     protected virtual bool SignalFilterAnswer(decimal IncomeValue) { if (VariableForSignal < IncomeValue) return StrDiff.ReturnAnswer(true, new int[] { 1, 2 }); else return StrDiff.ReturnAnswer(false, new int[] { 2, 3 }); } 
    • It does not work, in the BaseStrategy class BaseStrategy no StrDiff StrDiff = new StrDiff(); - K.Oleg
    • So add. Or not? - Vadim Ovchinnikov
    • Not all inherited classes will have StrDiff , it will also need to be changed as well as the StupidPaper to TimeFrame - K.Oleg
    • 2
      @ K.Oleg If the heirs have a bunch of different classes of fields that are closely dependent on each other, then this is a sign of bad architecture. Use Dependency Injection or Inversion of control. - Vadim Ovchinnikov