LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Looking for Delegation pattern,OOP example code

Solved!
Go to solution

Hi Everyone,

 

I am looking for Delegation pattern, OOP simple LabVIEW example to understand the concept.

Any example for delegation pattern, OOP.

 

 

 

0 Kudos
Message 1 of 5
(2,584 Views)

i have seen the article and downloaded the examples, but have not got understanding from the example, looking for some other example. 

0 Kudos
Message 3 of 5
(2,555 Views)
Solution
Accepted by LV_COder

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:

  • There is no common parent
  • There is a common parent but there are descendants that don't need the functionality and now they have to deal with implementing it - which can be a breaking change for users of those descendants

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.

0 Kudos
Message 4 of 5
(2,527 Views)

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. 

0 Kudos
Message 5 of 5
(2,509 Views)