GENERAL J ARTICLE
What are Objects? H N Mahabala
Object oriented development of software systems is becoming de-facto standard in large software projects. This article explains the motivation behind object orientation and how it promotes the use of software components. Objects as R e u s a b l e S u b s y s t e m s H N Mahabala retired from Indian Institute of Technology, Chennai in 1995 and is currently advising IT companies.
Computer software is perhaps the most complex artifact designed by humans.
RESONANCE I May 2000
Can you imagine, designing afresh and fabricating every unit required, such as pumps, compressors, fans, heaters, etc.; in a complex plant, say an oil refinery? Such an approach would not only take too long, but will also be prohibitively expensive; not to mention the risk of the newly designed equipment needing redesign. In fact, the manufacturer configures the plant using proven units, often available off-the-shelf. He concentrates on interconnection between units, instead of developing each unit all over again. This way the cost of the project will be minimal, since the suppliers of the units compete with each other. Interestingly, the units are not designed specifically for a refinery, but are designed for general use. Hence, the cost of a unit comes down because of large volumes being sold. Computer software is perhaps the most complex artifact designed by humans. A single instruction being out of place can disrupt the software. Software projects tend to get delayed, and some experts warn that a software development project, which takes longer than two years will most likely be aborted. Currently, users of application software are made to wait too long, and hence often lose interest in the project. Requirements may have changed due to changes in business practices with lapse of time. Every organization, which relies on computerization, dreams of getting operational software as soon as possible, once a need for it is established. In these days of instant coffee and other instant foods, it is not out of place to ask for instant software! Natural question in the minds of a user is, why did
48
GENERAL I ARTICLE
Objects are not designed specifically for a project, but are designed to be functionally comprehensive permitting wide reuse.
software developers not take a hint from the erectors of complex plants? Why not try to put together complex software as an assemblage of proven software modules or components? This will result in reduced development time and higher reliability because the components are fully tested and of course there is substantial reduction in cost. These reusable software components are called objects (see Box 1 for history of object oriented programming). Interestingly, as in the case of pumps and compressors, objects are not designed specifically for a project, but are designed to be functionally comprehensive permitting wide reuse. O b j e c t s as a C o n t r a c t o r Another view of the realm of software objects is from the world of contract services. These days, no one would think of organizing a marriage by planning and executing every activity involved. One contracts out catering to a catering contractor, decoration to a professional decorator, transport to a transport agency, etc. One does not have to worry ho~v the contractor gets the work done, except to give him/her clear instructions as to what is needed. Contractors have the freedom to execute the work in the most efficient way. What matters is that the Box 1. History of Object Oriented Programming
Object oriented(OO) programming was introduced by Xerox Corporation as early as the 70's. Researchers in the area of artificial intelligence (AI) wanted to imitate the way complex entities are organized based on a collection of simple function oriented entities. The first object oriented language was SMALL TALK, based on the LISP language. The inventors of SMALL TALK were confident that it would replace procedure-oriented languages. An article on SMALL TALK in the
Byte magazine had a cover with two
scenarios. Both showed an ocean with lot of islands marked with programming languages such as FORTRAN, COBOL, SMALL TALK, etc. In the first one, islands marked FORTRAN and COBOL were thickly populated, whereas the island marked SMALL TALK was very thinly populated. The second scene was reversed in terms of population, indicating that SMALL TALK would replace procedureoriented languages in about 10 years. This did not happen as early as predicted, but it is happening now. Reason perhaps was due to the limitations of early computers. OO languages provide a way to think of very complex systems by breaking them down into subsystems with a comprehensive set of responsibilities.
46
RESONANCE J May 2000
GENERAL I ARTICLE
contractors deliver what they promise. Interestingly, a contractor may use sub-contractors of his own choice to discharge his responsibility. Hence, the task of the person performing the marriage becomes much easier and he/she can concentrate more on attending to the guests and the marriage party. No wonder the availability of good contractors has enabled one to perform a marriage at a very short notice. Objects in addition to being viewed as components/subsystems can also be viewed as contractors who provide a set of stipulated (advertised) services. The main program uses the various services available from various objects to achieve its objective. Interestingly, it should not matter if the object is rewritten (say using a better algorithm) by the author, as long as services provided do not change. Since objects are fully tested before they are used, the software also achieves higher quality.
Objects in addition to being viewed as components/ subsystems can also be viewed as contractors who provide a set of stipulated (advertised) services.
Objects as Departments One can appreciate the viewpoint of objects by thinking of departments, which are organized for carrying out the work of an organization. An organization achieves its objective by setting up several departments, each of which is entrusted with a responsibility. One would avoid disputes between departments by not assigning the same responsibility to two departments. Thus, accounts department handles all financial matters, personnel handles all employee matters, transport handles all transportation needs, stores handles all inventory matters, etc. Each department provides a~set of well-defined services and is the custodian of data associated with those services. All ledgers of accounts are the exclusive property of the accounts department. Outsiders are not allowed to manipulate entries in a ledger, except through authorized requests to the accounting staff. This restriction ensures security of data as well and prevents misuse of ledger entries. An object also provides the same degree of security of data, as in the case of data held by a department, in that only operations explicitly permitted by an object can operate on the data they hold.
RESONANCE I May 2000
Only operations explicitly permitted by an object can operate on the data they hold.
47
GENERAL [ ARTICLE
An object is described (or specified) by listing the attributes it handles and the methods it supports on those attributes.
Objects and Subroutines It is natural for anybody who has used a programming language such as FORTRAN, COBOL or C to say that objects look very much like subroutines/procedures or that a collection of objects is similar to a subroutine library. The observation is correct, except that objects are multiple entry subroutines and their functionality tends to be more comprehensive and are chosen in a meaningful way. Users select objects based on the specification of the function they implement, rather than by going deep into the precise nature of the algorithm they employ. The complex nature of a graphical user interface (GUI) is achieved using an object oriented library of GUI widgets (specialized mini-objects).
Object/Class Model and Polymorphism
Figure 1. Any class~object. Class Name Attributes Methods
48
An application software basically manipulates and manages a set of data entities (data structures) such as personnel records, accounts, road maps, images, audio clips, etc. Application performs certain operations on the data entities to achieve their objectives. One way to compose objects would be to include all operations, also called methods, associated with a data entity and encapsulate the entire code into a multiple call subroutine or module. Since data often describes the properties (name, color, size, etc) of an entity, data which are manipulated by an object are also referred to as attributes. Thus, an object is described (or specified) by listing the attributes it handles and the methods it supports on those attributes (refer to Figure 1). For example, a savings bank account object handles attributes such as name of the account holder, account number, his/her address, current balance, etc. and provides methods such as opening and closing of the account, credit and debit operations, calculate interest payable, etc. One can learn to appreciate this concept of encapsulating attributes and methods to design useful objects by conceptualizing some common objects such as tickets, medical prescriptions, health records, pay roll records, etc. An object without meaningful sets of attributes and meth-
RESONANCE J May 2000
GENERAL J ARTICLE
a
b
Indian Airline Ticket
Indian Airline Ticket 10035
Ticket-Number, Name
10035, Mahabala Bangalore, Chennai, WL
From, To, Status Flight No., Date, Amount Issue, Cancel, Confirm Change
Figure 2a. Class template. Figure 2b. Class instance (object).
IC 952, 10/06/99, 2010 iIssue, Cancel, Confirm Change
ods on it will be just a bunch of code, which will be very difficult to use. In special situations, one can define objects with only data and associated read/write methods, or objects with only methods (data being obtained through calls to other objects). All airline tickets are similar in the attributes they hold. There is a need to differentiate between the concept of an airline ticket and a particular air ticket. One may view the concept as a template object (class) and an individual air ticket as an instance of it (refer to Figures 2a and 2b). A programmer defines an airline ticket object. Every time a customer buys an air ticket, instance of the airline ticket class is created with specific values for the attributes. The instance is identified with a unique ticketn u m b e r (or an instance ID) and all methods are invoked only on that ticket with a specified ticket-number (or an ID). Invoking methods on a class or template object is meaningless, since there is no definitive data for the methods to work on, just as a policeman cannot be asked to arrest a human, but only a particular individual (instance of a human with a name)! An OO application will operate on several instances of some objects. In some applications one can even create and destroy instances during run-time. Another apt example for objects is that of ships of a navy on the high seas. T h e y use different technologies from diesel to nuclear energy and the way one controls their movement can also be different. A commander who is directing these ships cannot be expected to remember the technology of each ship. Each ship
RESONANCE I May 2000
In special situations, one can define objects with only data and associated read/ write methods, or objects with only methods.
49
GENERAL ] ARTICLE
We refer to the feature of executing different codes for the same method-name as
polymorphism.
has its own attributes such as weight, name, manufacturer, weapons being carried, etc. In order that the commander can issue orders to a ship, every ship implements general movement methods such as 'move at N knots', turn right/left by X degrees, stop, etc. Thus, all ships can be viewed as instances of a generic ship (ship class). Further, the order to each ship in terms of invoking a method (say turn left by 10 degrees) is implemented using a sequence of operations that is specific to that type of ship. Thus, implementation (code to be executed) of a method is different for different types of ships. We refer to this feature of executing different codes for the same method-name (depending on the type of ship) as polymorphism. The spirit captured is 'when one wants a particular action (method) the receiver need not know or be concerned with the details of its implementation'. Another example would be that of ordering the lighting of a stove, which could be different for different types of stoves.
Objects Promote Reuse
Objects are a form of reusable code. One includes all
It was noticed very often, that certain sections of code in a major application could be used in other applications, since they implement commonly needed functionality, wherein some sections of the code can be reused. Reuse is an effective way to reduce development time. However, this reuse becomes easier if those sections of code were easily identifiable and can be slightly changed if necessary. Objects are a form of reusable code. One includes all methods and attributes one normally associates with a data structure to reduce the need for customization. Object Inheritance
methods and attributes one normally associates with a data structure to reduce.the need for customization.
50
Much as one would like to design objects, which have universal applicability, one may need to modify a method without altering the other methods or attributes associated with an object. There is a design feature called 'inheritance' which allows one to do precisely that (refer to Figure 3). It is somewhat similar to how inheritance works in nature. For example, a human inherits
RESONANCE J May 2000
GENERAL
I ARTICLE Figure 3. Inheritance.
Bank Account Account_No, Balance Name, Address Open, Close Debit, Credit, Balance?
Savings_Account
Current Account Overdraft Amount
Monthly_Interest
Issue_Overdraft
,J
many attributes (color of eyes, hair, etc) from his/her parents. Interestingly, only the instances of the derived (inherited) object can use the modified method (or additional attribute), whereas instances of the old object continue to use the unmodified method (or original attributes). Thus, the effort needed to customize an object will be minimal and involves no conflict with the original. Hence, reuse of a code (object), which sometimes needs slight modification can be done without meddling with the old code directly, thereby making reuse more frequent.
Object Oriented Design It is interesting to think about how one can describe the functionality of an object. Anybody asked to describe the working of an accounts department in a travel agency would describe what the department handles in terms of specific nouns (customers, tickets, destinations, etc.) and verbs (issue, cancel, etc.) Nouns used in the description are attributes and verbs generally signify methods. One methodology for designing objects involves examining the nouns in a textual description of an application
RESONANCE J May 2000
Nouns used in the description are attributes and verbs generally signify methods.
51
GENERAL J ARTICLE
C++ extends the programming language C by adding operations to define objects, instantiating objects and invoking methods on the objects.
for possible attributes and verbs for identifying methods. One does not stop just with nouns and verbs found in the description, but extends them further to achieve a comprehensive object. Object Oriented Languages An object-oriented language (OOL) is an extended programming language. For example C+ + extends the programming language C by adding operations to define objects, instantiating objects and invoking methods on the objects. An OOL enforces the discipline in use of the objects. One can achieve, to some extent, the benefits of an OOL by a non-OOL, by enforcing certain programming standards. Many companies implement object oriented design by using C or COBOL, by adopting necessary programming standards. Thus, it is not necessary to use an OOL to derive some of the benefits of object oriented methodology (mainly reuse). Full benefits of OOL such as easy customization and security are well worth the slightly degraded performance of OOL. Use of object oriented programming (OOP) is increasing and it is hard to think of designing an application using Internet without using the OO methodology. Programming language JAVA is object oriented and is ideally suited to implement Internet based applications. Objects written in JAVA, called applets allow one to download an applet from a server and execute it on any computer under any operating system. O b j e c t s in D i s t r i b u t e d C o m p u t i n g
It is hard to think of designing an application using Internet without using the O0 methodology.
$2
Computer networking has opened up a way for some applications to be implemented by executing programs on several computers distributed over a network. Distributed computing is sometimes necessary to use highly volatile (rapidly changing) data located at different places on the net. One interesting approach to distributed computing is to invoke objects on a remote server. A program in a client computer can send a message to a remote object (invoke a method) and get back the
RESONANCE J May 2000
GENERAL J ARTICLE
results. The client need only know the specifications (attributes and methods) of an object to invoke them. This remote execution is facilitated by message oriented communication software referred to as middle-ware. CORBA and COM are two prominent middle-ware used to develop distributed applications. Just as one uses a telephone directory to pick a contractor, one can choose objects catalogued in a directory located somewhere on the net. Client program need not know where the object is located. The middle-ware will send the message to the desired object wherever it is located on the net, even if it has been moved since the last call! Obviously, one can.look forward to a time when object providers will compete to get clients to use their objects and of course get paid on a pay-per-use basis.
Just as one uses a telephone directory to pick a contractor, one can choose objects catalogued in a directory located somewhere on the net.
Even though any object is capable of being reused, it was found that small objects turned out to be difficult to reuse, since they are d i f f i c u l t to i d e n t i f y a n d m a y also n e e d e x t e n s i v e customization. Reuse is now being made easier by designing a collection of complementary objects referred to as a framework. Thus, a health delivery framework will include all objects, mutually compatible, for taking care of almost all functions needed in a health delivery system. The concept of frameworks is further extended, by enhancing their functionality, into what are called components. IBM has designed a component library called 'Sanfrancisco' which will help one to put together a customized E R P (enterprise resource planning) package. Components are large, complex sets of objects, which can be viewed as reusable subsystems. It is expected that Internet enabled applications will, in course of time be component based. O b j e c t s are t h e F u t u r e
Man has always handled complex situations (organizing an enterprise or an event or fabricating complex p l a n t s ) u s i n g
Components are
'divide and conquer', in terms of logically isolated entities such as departments, contractors or components (electronic or mechanical). It is surprising that it took a long time for software deve-lopers to adopt this frequently practised divide and con-
sets of objects,
RESONANCE J May 2000
large, complex which can be viewed as reusable subsystems.
$3
GENERAL J ARTICLE
quer methodology by inventing objects. It is natural to think and plan in terms of objects. Object oriented development extends the divide and conquer concept into the realm of software development. Object orientation is being introduced even in old entrenched languages such as COBOL (object COBOL) and F O R T R A N (FORTRAN 95). Very soon, designing using object-oriented approach will be the standard approach.
Exercises: 1. Define the objects one could use to simulate the working of a travel agency. 2. Define the TA (travelling allowance) bill as an object. Address for Correspondence H N Mohabola 1408, 2nd Cross Ashok Nagar Bangalore 560 050, India. Email:
[email protected]
Suggested Reading [1] James Martin andJ Odell, ObjectOrientedAnalysis and Design, Chapter 2, Prentice Hall In~ Englewood Cliffs, NJ, USA, 1992. [2l Roger S Pressman, Software Engineering, A Practitioner's Approach (Fourth Edition), McGraw Hill, New York, USA, Ch. 19, Sec. 19.3, pp. 564 to 57I, 1997.
There is a largeness about mathematics that transcends race and time... The greatest mathematics has the simplicity and inevitableness of supreme poetry and music, standing on the borderland of all that is wonderful in Science and all that is beautiful in Art. Mathematics transfigures the fortuitous concourse of atoms into the tracery of the finger of God. H W Turnbull
Quantum Chemistry and Spectroscopy
54
RESONANCE J May 2000