Jump to content

What is the difference between a static & dynamic accessor?


Recommended Posts

When creating an accessor method, there is a choice:

Create dynamic accessor

Create static accessor

What is the difference between the two and how does that influence our choice?

Is it possible to change the type of accessor after having created the accessor? Or do we need to create a new one?

RayR

Link to post
Share on other sites

When creating an accessor method, there is a choice:

Create dynamic accessor

Create static accessor

What is the difference between the two and how does that influence our choice?

Is it possible to change the type of accessor after having created the accessor? Or do we need to create a new one?

RayR

Static and Dynamic refer to how the method is called. Suppose you have a Vehicle.lvclass and a Bicycle.lvclass, where Bicycle inherits from Vehicle.

If you wanted to add a method on Vehicle.lvclass that behaved one way for Vehicle and another way for Bicycle, you would use a dynamic accessor. Suppose it was ChangeGears.vi.:

Vehicle.lvclass

--ChangeGears.vi

Bicycle.lvclass

--ChangeGears.vi

You could pass a Vehicle wire into ChangeGears.vi, and if the actual type of the object on the wire were a Bicycle, it would call Bicycle.lvclass:ChangeGears.vi. If it were a Vehicle or any other class that inherits from Vehicle that doesn't override ChangeGears.vi, it would call the Vehicle.lvclass:ChangeGears.vi.

Static means you cannot override the accessor, and you only get the behavior defined at the top-level class. This has been (perhaps better) answered here.

You can change between static and dynamic by right clicking on the class in and class out in the connector pane and switching from Dynamic Dispatch to Required (for input) or Recommended (for output).

- Mike

  • Like 1
Link to post
Share on other sites

When creating an accessor method, there is a choice:

Create dynamic accessor

Create static accessor

What is the difference between the two and how does that influence our choice?

Is it possible to change the type of accessor after having created the accessor? Or do we need to create a new one?

RayR

Dynamic methods have dynamic dispatch input terminals for the object inputs and outputs of the owning class, meaning one can override those methods in child classes.

One cannot override static accessors, but one can in-line these VIs, for instance, to optimize performance.

It is possible to switch from one to the other by changing the property of the terminals on the connector pane, but I think it is far better to design appropriately in the first place.

In the design approach we use, static accessors are the correct choice in about 98% of the situations we encounter. There are a few rare situations where we need to access data via an interface, so we do define a dynamic accessor method on the interface (but the interface's method never actually executes). My advice is to use a static accessor method unless you are really sure you need a dynamic accessor method. (This answer may depend a bit on one's design approach, though. I think what I have stated is a good rule of thumb, but there may be other sorts of designs that would have different answers.)

  • Like 2
Link to post
Share on other sites

Hi,

I am still learning about LVOOP, but here is the little I do know.

A static dispatch VI is mostly a regular VI that is owned by the class.

A dynamic dispatch VI is sort of "similar" to a polymorphic VI. For each of these VIs, a replacement can be made for each child class. (A class can be a child of a class, of a class, ... etc) If the child class is used instead of the parent class, the "replacement" VI would be used instead of the original dynamic dispatch VI.

These replacements are defined by the same connector pane, and VI name. (The owning class name will be different)

The connector pane specifies if the VI is a dynamic or static dispatch VI. Right click on the class input/output connectors on the connector pane > "This connection is", and you may choose [required, recommended, optional, dynamic dispatch(required)]. If the dynamic dispatch option is chosen, the VI is a dynamic dispatch VI. If any of the other 3 are chosen, then it is a static dispatch VI. (Both input/output connectors for the class have to agree)

An accessor VI is a template for accessing the data from the class wire. This VIs are used as "gate-keepers" to the data. Making them dynamic dispatch allows for different behavior depending on the input class.

The dynamic dispatch VIs have a noticeable overhead during execution, and loading. It is always better to use a static dispatch VI unless your really need the dynamic dispatch.

PS. Darn....Paul replied first.....lol

Edited by SuperS_5
  • Like 1
Link to post
Share on other sites

Let's look a bit deeper into the meaning of static and dynamic. Remember text based languages with arrays.

Static: Known at built time, e.g. the variable is defined as array[1..10].

Dynamic: Memory is allocated at run-time when the specific operation is executed, e.g. CreateArray(10).

In OOP, this maps to the class/object relation. Static stuff is 'owned' by the class, while dynamic stuff is 'owned' by the object, e.g. memory is allocated when the object is created. If I remember correctly, in java I could declare an attribute as static, meaning all instances of the clas (objects) share the same attribute (so they all access the same data space). This is something that isn't possible (?) in LVOOP given the by-val/dataflow nature.

Making something static will make the memory footprint smaller, as the data space (both for attributes and methods) needs to be allocated once for all objects, while dynamic methods/attributes need to be created for each object.

I've mainly seen the usage of static declarations when there was no 'variable' (attribute) involved, like math functions (e.g. RandomGenerator.ReturnInt() would be static).

I've also seen that developers did split their classes into a dynamic 'normal' class and a static class that contained the static methods as a kind of library of helper functions.

Ok, that's my share of limited knownledge.

Felix

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.