labview中关于dynamic dispatch和static dispatch的使用范围

Static dispatching should be used

  1. for the initialization method of a class
  2. for high performance quickly executing methods executed more than ~ 100 000 times
  3. for methods you do not want to allow to be overridden
  4. for private methods
  5. for methods that do not contain class input terminal
  6. for methods you want to put inside polymorphic VI

Dynamics dispatching should be used

  1. for cleanup method of a class
  2. for recursively called methods (even for recursive init contrary to what was said above)
  3. for all methods you want to allow child class to override
  4. for all methods you are unsure if child class could benefit from overriding the method

Notice that you do not necessarily need to use dynamic dispatch output in dynamic dispatch methods. You can have dynamic dispatch input and a static dispatch output and a dynamic dispatch input wihtout any class type output. Normally you should use dynamic dispatch input together with dynamic dispatch output though. Use static dispatch output with dynamic dispatch input if you want a child class to return possibly other class type. See Aristos Queue's LVOOP Map as an example of this kind of metethods. Use dynamic dispatch input without class output if you want to clean up an object and the object is no longer valid after the method is executed.

Furthermore, If you're debugging code where all the method calls are dynamic dispatch, then you will always be uncertain about whether a particular call is dispatching to the class member VI you think it's dispatching to. There's a real mental cost to that uncertainty, and it will make your code harder to understand.

Using dynamic dispatch for everything can also introduce bugs into your code:

  • If you have static dispatch method Parent.lvclass:MyMethod.vi and accidentally create another method with a colliding name in a child class (Child.lvclass:MyMethod.vi) your Run arrow will break.
  • However, if you had MyMethod.vi set up as dynamic dispatch (because you decided to do everything that way), you wouldn't get any error from LabVIEW -- it would just happily start dynamically dispatching in places where you never intended it to do so.

你可能感兴趣的:(labview)