Code Reuse with Interface Design and Composition
In my last article I introduced some of the basic concepts behind object oriented programming. Today I continue this introduction by talking about code reuse and composition.
So what do we mean by code reuse? Quoting Wikipedia “Code reuse is the idea that a partial or complete computer program written at one time can be, should be, or is being used in another program written at a later time”. A traditional way in LabVIEW to reuse code is to write subVIs that are shared between many applications or multiple parts of the same application. SubVIs can be further packaged into libraries which contain multiple SubVIs handling different parts of some process.
Object-oriented programming takes code reuse a giant leap forward. The enhanced code reusability is built-in to the design of object oriented programming and was one of the main design goals of object-oriented programming. In my last article I did not mention code reuse. However the concepts I introduced in my last article are central for writing reusable object-oriented code.
Today I’ll concentrate on two concepts of reusing code in object-oriented programming. First code reusability is very much a design issue. In object-oriented programming reusable design is enforced by forcing developers to encapsulate the data and the methods for manipulating the data together into joint entities called classes. As a result programming interfaces become cleaner which as a result increases code reusability. In addition the object data is hidden from the users and only exposed via interface methods forcing developers to think how they classes expose their data to maximize reusability. Second existing classes can be used as private data members of new classes with similar functionality. This way the functionality of a new class can be partially or fully delegated to some other classes.
Object-oriented programming also introduces a powerful technique called inheritance that helps reusing code a great deal. It’s a topic of its own, and we’ll leave it as a subject for our next articles.
Designing Reusable Class Interfaces
Although object-oriented programming provides many ways for reusing code, it doesn’t mean that code is automatically reusable. Writing well reusable code requires both experience and a lot of consideration from the developer. An important phase of object-oriented development process is defining the interface for the class under development.
Last time we talked about class interfaces. An interface consists of a set of methods (subVIs) the class user can use to perform actions on an object of that class type. These methods can be for example manipulations of the object private data, getting information about the object state or interacting with other objects or other systems such as files, devices and network. When defining this interface, the developer should think carefully how to make these interface methods as general purpose as possible to allow the class to be used also outside the scope of the current application in development.
In the development process the development team should really concentrate on defining and specifying the central parts of the interface of each class. If the interface of a class is only an implicit result of coding and not a design decision, the code reusability will definitely be scarified.
Composition
If a class interface is well designed, the class may be easily used to implement some of the functionality of another class. This can be achieved by using an object of one class type as a private data member of another class. Using classes as private data members of other classes is called composition.
As an example consider a class Engine that represents an engine of a motor vehicle. The interface of Engine has methods Start Engine and Stop Engine. Now consider you would like to create a class Car. Cars normally have engines so you can now use the class Engine as a private data member of class Car.

You may want the interface of class Car have methods Start Car Engine and Stop Car Engine. Now that Engine is a private data member of class Car you can delegate the actions of starting and stopping the engine from the Car class to the Engine class.

Composition is a very powerful and flexible way of reusing existing code. A new class can be composed of multiple different classes. One class could be used as a private data member multiple times in the same class. In our example class Car could have an array of objects of class Wheel in addition to class Engine as its private data members. Composition even allows changing the underlying implementation at runtime but we will not go deeper in to this dynamic composition yet.
Conclusions
Object oriented programming brings code reuse to completely to a new level. Indeed better code reuse may be the single most important reason to migrate from conventional LabVIEW programming to object-oriented LabVIEW programming. In this articles I’ve introduced several mechanisms of code reuse in object-oriented programming. These seemingly simple concepts are however hard to master. In my future articles I’ll go into more detail how to master these techniques.
In the next article I’ll introduce third mechanism of code reuse called inheritance. Subscribe to the RSS feed not to miss it.
10 Comments
Make A CommentComments RSS Feed TrackBack URL
Leave a comment
You must be logged in to post a comment.

(3 votes, average: 4.00 out of 5)
March 26th, 2007 at 4:32 pm
my apology for asking this here if this is covered elsewhere please forward to appropriate forum.Is composition (of this type) actually stable in LVOOP today (ver8.2)?I have tried this before and it seemed to give me much trouble. Any pointers about what works and what does not work in 8.2 regarding composition?
March 26th, 2007 at 4:41 pm
LabVOOP is not stable by any means in LV 8.20. There are rumors of forthcoming maintenance release to 8.20 that should fix many of the LabVOOP related problems. If you are looking for a really stable platform, LabVOOP is not yet a proper choise. EDIT: Although there are stability issues, I’ve not encountered any or at most very few stability issues at runtime. Most of the issues are only present at development time. So if you get your code to compile, it very probably also runs correctly.
March 27th, 2007 at 12:38 am
Great article, Tomi! I really love the concept of "reuse" with respect to OOP. The way that I like to think about reuse in OOP, is that it enables reuse from the top-down rather than the traditional reuse from the bottom-up. Traditional reuse, as you mentioned in your article, is reusing subVIs and sub-routines across several higher-level components. OOP allows reuse to be inverted (reusing top-level components as opposed to lower-level components). Now, you can create reusable high-level components that operate on any lower-level (or sometimes collaborator-level) components that implement a known interface. For example, one can create a reusable stage control pad user interface that can operate on any type of stage that implements the pre-defined "stage" interface. Or, from your Car+Engine example, you can reuse a Car by putting in a new type of Engine. Very cool
March 30th, 2007 at 4:30 pm
Hey TomiI am a mostly self taught at programming, with just basic introduction level training in C++ and labview, and am very interested in object oriented programming. I was wondering where one would find some tutorials and examples for objected oriented programming for labview.ThanksBeaton
March 31st, 2007 at 8:06 am
Hi Beaton. LabVIEW Object Oriented Programming is a farily new feature and I'm afraid that there is no good material available. Indeed that is one of the reasons why I started this blog. I want to provide introductory information on LabVIEW Object Oriented Programming. There are many good books on object-oriented programming in general and in context of other programming languages. Consider for example reading Thinking in JAVA by Bruce Eckel.
April 17th, 2007 at 8:10 pm
Hi Tomi,Thanks for your nice explanation on reusing code by inheritance.Is there any way I can lay my hand on the Vis so that I can practice with them for a better understanding?.Cheers,Ohiofudu
April 17th, 2007 at 8:15 pm
Ohiofudu, I'm sorry I don't have example code at the moment I could provide you. Currently I've simply made fast prototype VIs for the articles and these are really not functional in any sense. But you're right it's an important way of learning and I consider providing example code.
April 27th, 2007 at 5:33 pm
Hi Tomi! I'm just experimenting some with LVOOP and there is one Thing that disturbs me: Consider a Car.lvlclass with an Array of Tyres.lvlclass. Now assume you would like to monitor the Pressure of your Tyres. So you will provide a Method in the Tyre to change the Pressure. But how to make SHURE your Car will know about it? The Tyre is – as usual - by Value, thus you have to make shure the changed Tyre will get back into the array of Tyre.lvlclass of the Car.lvlclass. The only secure Patern I see by now is: You wuould have to implement a Method in the Car for every Method of the Tyre. Or is there a better pattern?
May 7th, 2007 at 8:46 pm
Hi Stef. I think your idea was to have a GetTyre method in Car class. Then you could call a Tyre method outside the Car and as a by-reference object the Tyre of Car object would be modified or repressured".
I'm afraid this is not possible in LabVIEW Object Oriented Programming unless you create the by-reference system by yourself or get by-reference class template somewhere. I'm going to write more about by-reference classes and by-reference class templates later.
Another possibility is to use Command desing pattern. I'm going to write about this pattern later on as well in LabVIEW context. You can easily find general information on command design pattern from the Internet.
March 29th, 2011 at 1:35 am
[...] T. (2007) Code Reuse with Interface Design and Composition [Online]. Available from: http://expressionflow.com/2007/03/26/code-reuse-with-interface-design-and-composition/ (Accessed: 26 March [...]