Inhalte

Common Practices: Interfaces

Table of Contents

Query Interface

Each interface must inherit from __System.IQueryInterface. This can be direct or indirect by inheriting from another interface that has already inherited __System.IQueryInterface. Since TwinCAT 3 has no generic types, it is necessary to query interfaces because it allows us to, among other things, store data in generic data types like lists, dictionaries, and trees.

Segregation

If a class is used by different client classes, then it makes sense for there to be a suitable interface for each of these client classes. It is always better if a class implements many small interfaces than if the client classes have to deal with one ‘god-interface’. It is permissible to create a large interface for the class, which inherits from all the small interfaces. Common methods such as execute, run, enable etc. must each be in a separate interface. Otherwise, it becomes absurdly complicated to work with them.

Abstract Classes

Abstract classes are used to avoid code duplication. For the purpose of abstraction we use interfaces instead of abstract classes. Abstract classes are more difficult to use as abstractions in TwinCAT 3, as they can only inherit from one class and the validity of a reference to an object of an abstract class is difficult to verify.

Classes

Since we always work with abstractions to enable easy interchangeability of classes, it is necessary that all classes implement interfaces for their services (public methods and properties). This means that every class, whether abstract or concrete, that has methods or properties that are internal or public must implement one or more interfaces for these methods and properties.