Object Oriented Programming (OOP) and Graphical User Interfaces (GUI)

Object Oriented Programming (OOP) and Graphical User Interfaces (GUI)

W Monitor 251 Monitor Object Oriented Programming (OOP) and Graphical User Interfaces (GUI) Object oriented programming (OOP) promises a more effic...

469KB Sizes 3 Downloads 231 Views

W Monitor

251

Monitor

Object Oriented Programming (OOP) and Graphical User Interfaces (GUI) Object oriented programming (OOP) promises a more efficient programming tool to handle the increasingly complicated applications demanded by scientists. This introduces a whole set of new acronyms that may, at first, confuse the prospective user. In this article we will introduce the reader to this vocabulary and present an overview of the benefits of OOP. In traditional procedural programming, a program executes a sequence of algorithmic operations using control structures such as conditional branches and looping. The procedure calls and data are kept separate. With structured programming methodology, well-organized programs can be written. In OOP, an even more modular approach is undertaken. The whole problem is considered as the relationship between objects. The corresponding program handles logic flow quite differently from the traditional approach. Modules which combine both data and the codes that manipulate them exist and com-

putational activities are carried out by passing messages between these entities. Each entity models an object in the real-world target. For example, in data acquisition systems the analog to digital converter (AD0 will send messages to the data collection buffer, and the data collection buffer will send messages to a plotting routine. This example of a laboratory automation application project will be used throughout to illustrate the principles behind OOP. Basic concepts

and

terminology History OOP has its roots in the language Simula67 designed in the late 1969s for complex simulation problems. Smalltalk was then developed by Xerox Corporation’s Palo Alto Research Center, constituting the first complete programming environment built around the concept of objects. In recent years, object oriented constructs have been grafted into existing procedural or functional languages like C and Lisp to become Objective C and Flavors. An object is a unit having a well-defined data structure (also called state or representation) coupled with a set of operations that describe specifically how the

data can be manipulated. The executable code sections contained within an object are called methods. These determine how an object can be manipulated. Methods can be invoked by receiving sent by messages others, resulting in a change in the state of the objects. Through message passing, objects can communicate with each other to exchange information. To create a collection of objects with a specific data type and common operations, a template known as a class is used. A class performs as a prototype for new variables. Its definition states the structure of the data, and the operations that may be performed on its representations. You can create an object called a meter that will define the data structure for a meter’s reading and how it is going to be displayed on the screen. It will be called a meter class. To declare variables of this meter class, objects are created via the template, and are referred to as instances of the meter. A meter for temperature and another for pressure would be instances of the meter class. They share the same methods and data type and have uniform behavior. Encapsulation Encapsulation describes the process by which the data are

252

combined with the procedures or functions that act on them. With encapsulation, information hiding or data abstraction in OOP terms, is achieved. In contrast, traditional programming approaches use procedures that are applied to passive global data structures. In the OOP approach, they are packaged together with encapsulation. You do not need to worry whether the operation would work correctly on the data type or not. That is why this type of object is also called an abstract data type. You can manipulate an object using its available methods by message passing. Data abstraction allows you to use a data type without being directly involved in its internal structure. For instance, an illustrative example of information hiding is the use of an ADC. You trigger the control of it to start a conversion and receive a digital value in return. However, you don’t need to know how the ADC is built or whether it is based on a successive approximation or dual slope architecture. You do not need to know how the data are stored and whether it is in an array or a stack. This can simplify program enhancement and reduces program complexity, especially in programming problems like graphical user interfaces (GUM whose programming may involve thousands of function calls 111. Each object can be optimized individually without affecting modules that access it. In order to prevent the data abstraction from being tampered with by internal updating, some languages provided accessor functions in the external class interface section for those attributes that may be modified by clients.

-

Chemometricsand IntelligentLaboratorySystemsn

These may only be invoked through message passing in ways that ensure data integrity. This member protection scheme may bs applied to general objects or inherited objects, which are introduced in the next section. The scope of access allowed for the internal data of a class member is sometimes referred to as visibility. To protect private data that are to be hidden from public messages, private messages that change private instance variables are prohibited from calling objects other than the ones to which they belong. Inheritance Inheritance allows a new object to be built upon an existing one which models real-world hierarchical relationships. The object resulting from inheritance is known as a derived type. In essence, a derived type combines its own unique attributes with those of the class from which it was derived. Let us use the GUI for the data collection project as our example. A generic window class can be used for displaying data on the screen with scroll bars and resizing buttons. To display temperature measured from a thermocouple, a window object can be derived from the generic window class. Inheritance is then used to create a specialized class window with dialog boxes and pull-down menus for creating objects for intelligent instruments which need means to enter parameters. This class would inherit all the general window characteristics from the parent. Only new codes for individual new features have to be added, without rewriting any of the codes in-

herited from the general window class. In OOP terminology, the generic window class is called the superclass or the ancestor type while the derived class with dialog boxes, etc., would be a subclass, child type or descendant type. Depending on the visibility given, not all the parent’s variables may be accessible for data abstraction purposes. If a class contains methods for its subclass but has no data type, and no instance is made from it, it is called an abstract class. The use of an abstract class can simplify program structure and maintain the hierarchy in cases when data type inheritance is not desirable, but methods must be shared between its subclasses. Both an &bit ADC and a 24-bit ADC may need similar control codes; however, the data collected can be represented by an integer and double length words respectively. In this case, an abstract superclass could be used. Single inheritance objects have only the characteristics of one superclass. It is possible to implement a more complex scheme of multiple inheritance. For example a class is allowed to have several ancestors and has the characteristics of the parent data types. This is more flexible, but issues like conflicting variable names and methods have to be resolved. Polymorphism Polymorphism is the capability of class methods, when inherited by their descendants, to adjust their behavior depending upon the requirements of each child instance of the class. The term is derived from the Greek

n Monitor

253

words meaning “many shapes”. Thus, different objects can respond ditrerently to the same message. In responding to a plot message, objects from the same class spectrum can have different behavior. If the receiver is an infra red (IR) object, it will plot itself in transmittance versus wavenumber while a generic nuclear magnetic resonance (NMR) object will be drawn with intensity against chemical shift. The ability to redefine an inherited method is called overriding or overloading. For instance, a 13CNMR (CMR) object can inherit methods from the generic NMR object discussed above, which has all the necessary operations for this type of data structure. However, the plotting routines require a different range of chemical shifts. Instead of creating a CMR object from the spectrum class, the CMR object may simply replace the plotting method and substitute its own codes while using the same name for that method. However, the superseded definition of the ancestor can still be invoked by alternate means.

special tool called a constructor to initialize an object containing virtual methods. They are then disposed by a destructor tool that frees up the allocated space. Dynamic objects are created on the fly. This is necessary since some of the object types are not known until run-time. This may slow down operations but offers more flexibility and keeps the program size smaller. As objects are created during execution, some of them are only necessary for local declarations or temporary storage. These can be discarded later. The mechanism to collect all the unused objects and release the memory space allocated to them is called garbage collection. The lifetime of each object is referred to as the object’s persistence. Usually, the objects would only be presented for the time of the program. However, in database applications, the database itself and its contents will have a persistence longer than the individual programs that use them 121.

Binding Different language implementations have different ways to handle the method calls. All the data types and all references to methods can be determined and ‘bound’ together by the compiler at compile time. The approach is called early or static binding. This is commonly used in traditional programming languages. This allows faster execution but may also increase the final code size. In late, or dynamic binding, references to the virtual methods are resolved and bound at runtime. Some languages require a

Reusability The advantages of object oriented programming include code reuse, extensibility and ease of development and maintenance. OOP promises an increase in productivity because it allows you to reuse objects and classes. This code sharing can prevent programmers from reinventing the wheel and speeds up the production cycle. In the case of data collection, if all the modules are objects themselves, rewriting any one of these is not going to affect the whole program. The internal data

Advantages

structure is not important as long as it handles the messages correctly. Therefore porting the software over different hardware platforms will be less painful as long as the low-level, hardwarespecific functions of the system are encapsulated into abstract data types. As an example, any substitution of an ADC board will not affect the program as a whole, as only those low-level objects involved have to be rewritten. The high-level objects like signal processing or graphics which are either inherited from these or communicate through messages will not have to be changed as they see no difference in the modified objects. Reuse of the mGority of the code will shorten the development time needed. Extensibility Objects can be easily modified and combined; for example, objects developed to display chemical structures along with plots can be reused later if you want to write a scientific word processor. Since programs execute by passing messages between objects, it is easier to add new functionalities to the existing applications as long as they can respond to the messages correctly. For example, to add a new mass spectrum (MS) object to a laboratory information system, the MS object needs only to know how to plot the data on the screen similar to the current IR and NMR objects. It will then fit into a system that previously only had IR and NMR capability. This is referred to as differential programming or programming by modification 131. The feasibility of supporting multiple platforms and the use of a basic architecture that is easily

254

updated to meet new specitications can extend the software’s lifetime. Program maintenance As modules of a project are more finely defined in OOP, the division of work between individuals for a large application is more easily attained. Each person is only responsible for his own program modules as long as the interface to the rest of the system is clearly defmed at the start. Moreover, under OOP, programming problems can be traced back through the hierarchy tree. If there is only a problem in displaying of an IR spectrum, programming mistakes probably are in those objects associated with the IR. If, on the other hand, none of the data can be printed out, the bugs are more likely to reside inside the generic printing objects. A less obvious, but important, advantage is that the use of multiple IF or CASE statements is no longer necessary for the codes to manipulate the data types correctly. The readability of OOP codes is therefore superior to traditional approaches; the more the code resembles the real-world undermore the problem, standable it is. Object-oriented Programming Languages Languages with OOP implementation can be divided into two categories: pure and hybrid. Pure OOP languages are designed

Chemometricsand IntelligentLaboratorySystemsn

from the ground like Smalltalk. The more common implementations of OOP today are hybrid. In hybrid languages, OOP capabilities are put on top of familiar programming languages, such as C and become a language like C++. This approach lets programmers familiar with the base language continue to use existing source codes and programming techniques while gradually learning OOP with less frustration 141. Since numerous OOP languages have been developed with varying degrees of OOP capability implementation and it is not the intention of this article to review all the different OOP language approaches, interested readers are referred to the corresponding references M-71. Conclusions An OOP is more than a language. The perspectives of the programmer for the problems and the applications can be more important than the program language tools, which are only a means to achieve the goal. In the same way that using a language or program without GOTOs does not mean the program will be structure programmed, a program written in language-supporting objects may not be object oriented. On the other hand, good program organization, with data and program functions linked together in a non-OOP language, can be viewed as object oriented and enjoys its benefits. In addition, a system that lets you manipulate objects with a mouse

and resize windows does not necessarily mean an object orientedsystem -a common myth. Like any other programming methodology, the best way to learn OOP and GUI techniques is to get hands-on experience with them. Try a hybrid first, with a familiar face. The whole world of OOP and associated GUIs is open and waiting for the reader to explore.

REFERENCE3 1 Z. Urlocker, Object-oriented programming for Windows,BYTE, 15 (May 1996)237-294. 2. A. Hazzah, Objects are taking shape in a flat relational world, Sof’twareMagazine, 10 (June 1990) 32-42. D. Thomas, What’s in an object, BYTE, 14 (March 1989)231-240. S.R. Ladd, R. Relph, W. Ketiel and D. Chalmers, Objects of attention, InfoWorld, 12 (30 April 1990) 59-68. J.H. Saunders, A survey of objectoriented programming languages, JOOP, 2 (March/April 1989) 5-11. J. Micallef, Encapsulation, reusability and extensibility in objectoriented programming languages, JOOP, 1 (April/May1988) 12-36. P. Wegner, Learning the language, BYTE, 14 (March 1989) 245-253.

CHING WAN YIP and RAYMOND E. DESSY Department of Chemistry, Virginia Polytechnic Institute and State University, Blachsburg, VA 24Of31-0212, USA -