LabVIEW Development Best Practices Documents

cancel
Showing results for 
Search instead for 
Did you mean: 

Hierarchy Composition Pattern

(adapted from Gang of Four’s Composite pattern)

Intent

To represent a single object as a tree of smaller instances of that same object type. Useful for images (an image is made of a set of sub images), file systems (directory contains both files and other directories) and other systems where there is a common aspect to both the whole object and its parts.

Motivation

“Composition” refers simply to using one class as member data of another class. One class is thus “composed” of another class. The Hierarchy Composition pattern is specifically for times when you are making a class that is going to be composed of smaller instances of itself.

Implementation

An example of this pattern shipped with LV8.2. It was significantly revised in LV8.6. See

<labview>\examples\lvoop\Graphics\Graphics.lvproj

This example includes a class hierarchy for Graphic.lvclass, as shown in the image.

A graphic is simply something that can be drawn. Graphic defines a method Draw.vi, which can be overridden by children. There are three children of Graphic. The first three are straightforward implementations: Point.lvclass, Line.lvclass, and Circle.lvclass. Each of these has coordinate data as its member data, and implements Render Picture.vi accordingly. The third class, however, is Collection.lvclass. Collection has as its private data an array of Graphics. That is, Collection, which is itself a Graphic, contains other Graphics. Its implementation of Render Picture.vi is to loop over that array and call Render Picture.vi for each contained element. The demo shows how a Collection graphic can be built up from individual points, lines, and circles.

You could continue this, adding a Collection inside a Collection, nesting them to create more complex Graphics.

The difficulty arises when you try to call Render Picture.vi for a Collection that contains another Collection. This is a recursive call to Collection.lvclass:Render Picture.vi. As of LabVIEW 8.5, LV does support recursion as long as you make the dynamic dispatch VI reentrant using the “Share clones” option.

Editorial Comments

[Stephen Mercer] The ability of a class control/indicator to contain itself or any of its descendant types makes a LabVIEW class very different from a class in C++ or JAVA or most other object-oriented languages. LabVIEW has a mechanism that allows us to have this hybrid of one class directly containing another class with one class containing a reference to another class. We have a by-value syntax, but the containment rules behave like references in most other OO languages.

Elijah Kerry
Chief Product Manager, Software Platform
_______________________________________________
Follow my Software Engineering for LabVIEW Blog
Comments
Member Andreik_ee
Member

Where is the pattern implementation to download?

Active Participant cirrusio
Active Participant

Doesn't he give the path in the above comments to the example file provided with Labview?  Also, check out this discussion over here on sequences; I used a composite pattern for this class.  Also, check out the wiki on composite patterns.

Contributors