Tuesday, January 19, 2010

LabVIEW OOP Singleton Reference Example

If you use LabVIEW classes in your application, examples, POCs, etc., you may have been wondering how to implement a singleton object.

What is a singleton? Singletons are known in the LabVIEW world as action engines, functional globals with methods, and under a few other names. Basically it is a set of data with associated functions, and you ever only one instance of this object in your application. A functional global (non-reentrant subVI), with uninitialized shift registers (USR)) to store the data and multiple cases to implement the different functions, is the most common example of a singleton in LabVIEW. Using OOP you can easily create a class which contains the same data and functions, however, by default a class will not limit the application to only one instance (object) of the class.

There recently was a good discussion on LAVA about singletons, including a good reference example of a singleton implemented as a LV class provided by LV R&D.

http://lavag.org/topic/11513-singleton-design-pattern/page__view__findpost__p__70428

One application of a singleton object may be a database interface in an application where many different components or other objects access the same database. Typically you only want one object or component touching the database to avoid conflicts, race conditions, etc. A database access class with a singleton object is a good solution for this type of application.

No comments:

Post a Comment