09-18-2018 06:48 AM
Hi Everyone,
I am looking for Delegation pattern, OOP simple LabVIEW example to understand the concept.
Any example for delegation pattern, OOP.
Solved! Go to Solution.
09-18-2018 08:08 AM
Have you seen this article
https://forums.ni.com/t5/LabVIEW-Development-Best/Delegation-Pattern/ta-p/3491604
09-18-2018 08:14 AM
i have seen the article and downloaded the examples, but have not got understanding from the example, looking for some other example.
09-18-2018 04:39 PM - edited 09-18-2018 04:41 PM
This is a fairly basic pattern. Its not even really a pattern; more of a "foundation" of many patterns really (as Stephen suggests in that link). The idea is that classes obtain new behavior by composition rather than inheritance. Here is a simple run-down:
Class A and Class B want some specific functionality that is not directly related to their core functionality. Inheritance could be used to add this to a parent in the inheritance hierarchy but this might not be feasible for a number of reasons eg:
Class A and Class B instead use an instance of Class C which has this special behavior. They keep a copy of Class C in their private data and, when they need the special behavior, they ask that Class C instance to perform it on their behalf.
This is an example of the old adage "Favor composition over inheritance". Delegation is how we achieve that. If you are still a bit stuck with the concept take a look at the "Strategy" pattern which uses this precise idea.
09-19-2018 05:42 AM
Simple example:
There's a HW device A that has a TCP\IP interface, and a RS232 interface.
So you could make a parent class, and give it two childs, Device A (TCPIP).lvclass, Device A (RS232).lvclass. That works, and since Device A (TCPIP).lvclass and Device A (RS232).lvclass are both Device A's, it's a show case for inheritance.
Then you get a device B... It has TCP\IP and GPIB...
Then it starts to make sense to use delegation. So there would be an interface.lvclass, with TCPIP, RS232 and GPIB as childs. Now both devices can choose which interface to use. They could even switch during run time, something not (easily) possible with inheritance.
I'm used to calling this a decorator, BTW, which is a specific type of delegation. A Decorator is in the class' data, a delegation could also mean a class is used as input to a method. For instance, a HW class acquires data, that needs to be filtered. It could implement a Return Filtered Data method, that accepts a Filter.lvclass as input. There's no containment, just a simple input on a VI.
I use both a lot. I'd say it's almost harder to find a class that doesn't use any delegation! But this is used so often, it's hard to come up with a "typical" use case.