|
|
|
|
|
Object Oriented Programming CodeUsing code to break down a computing problem into discrete units.The aim of this article is to introduce the reader to basic Object Oriented principles in a language neutral way.
IntroductionIn the Abstract Data Types tutorial, we looked at how ADTs can be used to model specific pieces of data and operations on that data in a way that is implementation non-specific. Object Orientation extends this concept to provide a way of breaking down the programming problem into discrete units - objects - which totally encapsulate the ADT and all its associated functionality, but which are designed to interact to create an entire system. OO DesignObject orientation has to be approached from the design aspect first, as we need to know what objects will make up our system and what interfaces they will provide, before we begin to implement it. It is at this stage that we will also decide upon the granularity of the desired system, as well as what objects we can reuse from previous projects. This is not to say that OO is only valid for large systems, just that, as a paradigm, it must be thought about before executing the implementation, as mistakes can be hard to correct afterwards. For example, should we decide that, in our system, we can store a book as a single document, it would be difficult to undo this decision during the implementation if we suddenly found that, in order to print it, we would have to think in terms of pages. In general, programmers might find it more useful to think in terms of the smallest possible object, and work up a system from there - books are made of pages, which are made of sentences, which are made of words, which are made of letters... Each object needs to be defined in terms of the data that it is expected to manipulate (it's own ADT, or in terms of other objects in the system), and the methods that it will expose to other objects in the system that might want to use it. We say that the information is hidden, or encapsulated, within the object, and the only way to access it is to invoke a specific method designed to return the data in a specific format. This format is likely not entirely the same as the internal representation; but as a programmer, this information is not needed at the point of interface. OO ProgrammingImplementing the OO design requires that we create classes to define the objects, and instances of those classes for the run-time elements that interact to provide the system. Each class will need a way to initialize itself (the constructor), a way to destroy any memory that has been used in storing its internal state, a definition of the data that makes up its internal state, and a method for each interaction that the object permits. This entails a get/set data method for each kind of data that the object manages. For an address book application, each address might be contained within an object that exposes methods for getting and setting the Name, Address, Telephone Number and so on. The Address Book object itself might contain many addresses, along with basic search functionality, allowing us to envisage an interaction such as: oAddressBook-FindEntryByName(szName)-SetPhoneNumber(szNumber); (The above is C++ syntax-specific, but other languages will allow similarly styled expressions). ConclusionThere is a lot more to OO analysis, design, and programming, but the above gives a good grounding in some of the principles and terms used in this complex area of computer programming. Generally speaking, advanced programmers will actively use object orientation, while novices may decide to use part of the principles and arrive at a way of working that is comfortable for them. For industrial programming, using OO techniques helps build up a library or reusable objects, with the benefit that they are tested and have well defined, predictable behaviour, and can be used immediately. Over time, this will mean an increase in productivity, while the initial effort might be considered higher than when using other techniques. Navigation Links
Mailing ListStay informed - sign up to the mailing list!
The copyright of the article Object Oriented Programming Code in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Object Oriented Programming Code in print or online must be granted by the author in writing.
|
|
|
|