We will copy and paste this definition to start our story. And after reading the definition, a thought creeps in, how can object-objective programming (OOP) be implemented on a PLC?
In fact, it's easy. And a lot of such patterns can be put on the rails of industrial programming.
To implement this madness, we need Codesys 3.x because he did not learn much in OOP. Let's transgress.
REALIZATION
What do smart articles on the internet offer us?
The Strategy pattern proposes to define a family of similar algorithms that are often changed or extended, and move them into their own classes, called strategies.
Instead of the original class executing this or that algorithm itself, it will act as a context, referring to one of the strategies and delegating the work to it. To change the algorithm, it will be enough for you to substitute another strategy object into the context.
It is important that all strategies have a common interface. Using this interface, the context will be independent of the specific classes of strategies. On the other hand, you can change and add new kinds of algorithms without touching the context code.
INTERFACE
The interface, according to the Codesys documentation, is an object-oriented programming tool. An ITF ("Interface") object describes a set of method and property prototypes. In this context, prototype means that methods and properties contain only declarations and do not contain implementation.
This means that we describe methods in the interface. We give them names, a list of input and output data, but do not touch the implementation in any way.
Creating an interface
To create an interface, press RMB-> Add object-> Interface, then give it a name. I will have it "StrategyInt".
Then we add the method. Let's call it GetSTR. To add also right mouse button and add interface method.
We register only one output variable there.
Description of interface method variables.
Now, when we create function blocks inherited from this interface, we will already have a GetSTR method with an output variable OUT in it.
CONTEXT
This is the functional block that we will call various "strategies".
We create a regular function block.
A functional block that plays the role of a context.
We can send any object that will inherit from the StrategyInt interface to the input of the function block. In the implementation, we see that when calling this function block, we will call the GetSTR method, and return the result to the Result output variable.
STRATEGIES
The next step is implementing strategies. First, let's make two. They won't be any different other than returning different values.
We create a functional block and inherit it from our interface.
Function block creation window.
You can specify any name, I just ran out of fantasy.
I have created two such blocks, which will play the role of our strategies.
Strategies created
Each has two methods, the implementation of which needs to be written.
Implementation of the first strategy
Implementation of the second strategy.
Yes, you can directly return values through a method, but I don't want to.
ASSEMBLY
And now we put it all together.
Assembly of all components.
I broke it down a little in steps. For implementation we need:
Object that plays the role of context - 1 pc. Our strategies. Here I included them in the block of variables, but you can also make a pointer to the object of the strategy we need, and create it in the code via __NEW. Well, the variables Out_1 and Out_2 for clarity.
The code works as follows. In the first step, we give our strategist POU_1 as input to the Object variable MyProc.
Then the context is called and it works by writing the result of its work to the Out_1 variable.
Then we give the POU_2 strategy to the input of the same context, with the subsequent output, the result is written to the Out_2 variable.
Turn on emulation.
Execution result.
In this way, you can spread the logic of the operation of objects that are similar, but slightly different in implementation. Returning a string variable here just for example)
Additionally, to support this article, I paid for a static IP and created a port forwarding scheme on the Mikrotik router. You can connect and use

The author of the article: Alexander Sudalin - sudalin.kms@gmail.com
#OOP, #CODESYS, #Onlineexample, #PLC210, #Strategy, #Design