3970
Introduction to Objects
This help article provides background information that may be helpful for those new to object oriented programming in IDL.
An example of using object arrays to perform data I/O and image displaying can be found in the help article Programming with Objects: Basic Examples Using I/O and Images.
In general, what are Objects?
Object Oriented Programming provides a way of creating user-defined, self-contained, portable applications.
What is an IDL Object?
In IDL, objects are a special type of heap variables (global variables with explicit control over their lifetimes) that are accessible only via object references, much in the same way data is referenced by a pointer. They are made up of structures housed within heap variables that are accessed through object references. These structures, known as "classes", can contain fields of any IDL data type including other objects. IDL has many built in objects, for example the objects used in IDL Object Graphics. Users also have the ability to define and create their own objects.
The OBJ_NEW function creates an instance of a given object class. With each new instance of the class, a new instance of the data associated with the class is created. This data is contained in a heap object variable and can be manipulated using the "methods" defined for the class.
What are the properties of IDL objects?
- Inheritance- An object can inherit the behavior of another object, i.e. its methods and data, thus reducing the coding necessary to provide extended functionality. A class structure that inherits from another class structure is defined as a "subclass" of the other class. The class that is being inherited from is called the "superclass" in this case. Adding an inherited class to an object class definition is equivalent to adding all the fields in the superclass's definition. The additional advantage is that all methods are also inherited.
- Encapsulation- Objects are self-contained. Users can access an object only via the methods for the object's class. An object of a certain class is invisible to the methods of other classes, unless those classes were inherited. In that case, then the object can use both its own and its inherited methods.
- Polymorphism- The fact that object data is hidden from methods of another object allows multiple object types to support the same operations (methods), such as PRINT. This allows for single applications to be written that may need to operate differently in different contexts. For example, a printing object could be defined with a PRINT method. Depending on the platform the user is on, or the device the data is being printed to, PRINT can mean different things in each context. However, with objects, all the user need know is that the PRINT method exists, and that could be to a file, a printer, a plotter, etc. They need not know anything of the internals. The proper method will be applied to the object for the situation, as defined internally. All is transparent to the user.
- Persistence- Objects remain in memory until they are explicitly destroyed. Once an object is created you can alter any aspect of it without recreating the entire object. For example, in Object Graphics to alter a plot, you simply modify the portions of the plot you desire. All other properties will remain as rendered. This is very different than in Direct Graphics, where you would need to recreate the entire plot. An important result of the resiliance of objects is that the user must remember to explicitly destroy the instances of the objects that they create. Otherwise, there will be memory leaks. It is also important to understand that while an object will exist in memory, it is only accessible by the object reference. The object reference, much like a pointer can be passed between routines to reference the object with only one instance of the object ever being present in memory.
Skeleton of objects:
- Init method
function IDLmyOBJ::Init, _extra=extra
; All classes must have an Init method (function).
; this method will be called when an
; instance is created using OBJ_NEW.
...
; should call the superclass and pass along keywords
return,self->class2::init(_extra=extra)
end
- Cleanup method
pro IDLmyOBJ::Cleanup
self->Class2::cleanup ; call superclass
end
- Create various methods for the object
pro IDLmyOBJ::Method1
....
end
pro IDLmyOBJ::Method2
....
end
function IDLmyOBJ::Method3
.....
return,...
end
- Define the object class
pro IDLmyOBJ__Define
struct={IDLmyOBJ, $
inherits Class2, $
..., $
..., $
}
end
Then, save the code with the same name as the definition procedure with a .pro extension, so in this case idlmyobj__define.pro.
What are examples of situations where objects can be useful?
- Printing
- Visualization
- Arrays
- Mathematical functions
- Structures
- Pointers
Objects are useful anywhere you want an easily reuseable, encapsulated application. The user is provided access to the functionality of the object without their needing to access or understand the internals of the code.
Where can I find examples?
The help article Programming with Objects: Basic Examples Using I/O and Images has an example of defining an object and some basic methods associated with it and then includes a sample code which creates an instance of that object.