LabVIEW Development Best Practices Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

Any sample of observer pattern

I am learning the book Head first design pattern. The 1st introduced pattern is observer pattern.

I saw many samples of different patterns here. Is there one of observer pattern.

Also I may want to make a small unit to read several sensors with observer pattern.

Thanks.

0 Kudos
Message 1 of 2
(14,858 Views)

Hi alex. -

 

The Head First book is an excellent starter text! It explains several of the most-used patterns for OO programming in text-based, reference-based languages like Java. The Observer pattern is definitely one of these: it's used to propagate state changes among objects by notifying interested objects of a change in state. It does this by managing a list of references to those objects, so it can execute a public method in each of those objects that lets them respond appropriately.

 

Since LV is natively a by-value language and not a by-reference language, the Observer pattern doesn't naturally fit. However, with either Queues (for LV 8.6 or earlier) or Data Value References (for current versions of LV), we can create objects and wrap them into a pointer to give them some by-ref behaviors Using this technique, we can, in effect, create the Observer pattern.

 

We haven't yet written an example of this implementation, mainly because it is best achieved using one of the by-ref OOP add-ons for LV. These add-ons provide tools and wizards that let you create by-ref classes easily. The classes usually have a DVR or other memory pointer inside the class, with extra logic for managing race conditions, deep copy operations, lazy and early loading, etc. Two that I can name off the top of my head are GOOP and G#. I know that GOOP has a tool for generating common design pattern code, and that Observer is in the list of patterns, so you could probably start there to see code that implements the Observer pattern. G# may also have this capability; I haven't worked with it at all.

 

I'd like to add that the Observer pattern, like all design patterns, is useful for achieving a set of pretty specific goals. If you have an object whose changes in state will affect other objects, and you're coding with by-ref objects in your design, then you can use the Observer pattern to notify observing objects of a state change in your subject. When I studied the Observer pattern for awhile, I found myself making the mistake of trying to use it for general broadcast messaging among components in my application. While it could be used for this purpose with some success, I found the lack of an active process in the design made it difficult to implement that way. I feel there are other ways to design a messaging system -- that is, a code module responsible for relaying communication among actively running processes in an application -- using native LV features. I hope to write an article on that topic sometime before NI Week, wherein I'll compare various types of messaging needs and some existing approaches to meeting them.

David Staab, CLA
Staff Systems Engineer
National Instruments
0 Kudos
Message 2 of 2
(2,912 Views)