Inhalte

Common Practices: Dependencies

Table of Contents

Introduction

In an idealized software, there are only dependencies to interfaces or to abstract classes, but never to concrete implementations. This is not so true because we have classes of TwinCAT, which we include directly and do not work with abstractions, e.g. TON, TOF, F_TRIG etc.. For these classes there are no abstractions either, but this is not so bad for us because it is very unlikely that they will change. But in everything we develop, we are always working with abstractions, or more precisely, an interface. Abstract classes are there to take the implementation work off our hands, but abstract classes also have to implement their own interface. The following rules must be observed. For easier reading use concrete classes, but of course except the TwinCAT classes.

Do not use concrete classes in your classes

The code should use abstract interfaces instead of concrete classes. For objects that are created at compile time, the dependency must be realized via dependency injection. For objects that are created at runtime, abstract factories must be used.

No derivation from concrete classes

Inheritance represents the strongest binding in code. Therefore, for classes of which there are several derivatives, abstract classes should always be created first so that it is easier to adapt classes without it having an effect on subclasses.

Do not override methods of concrete classes

When overwriting methods of concrete classes, a dependency is created by inheritance, so here too: first create an abstract class and derive the necessary subclasses from it. This way the different functions can be implemented easily without creating dependencies to concrete classes.

Do not waste your time

If you understood the three rules, then the chapter is done. The remaining chapter explains just dependency injection and abstract factories with examples. It contains no other rules! You do not need any examples, then just proceed with https://ekvip.atlassian.net/wiki/x/IABVYg