understanding of OOP. They are starting with a clean slate. If you've beenprogramming in procedural languages for many years and C# is your first object-oriented language, the best advice I can give you is to keep an open mind andimplement the ideas I'm presenting here before you throw up your hands and say, "Ican fake this in [insert your procedural language of choice]." Anyone who's comefrom a procedural background to object-oriented programming has gone through thislearning curve, and it's well worth it. The benefits of programming with an object-oriented language are incalculable, both in terms of writing code more efficiently andhaving a system that can be easily modified and extended once written. It just mightnot seem that way at first. However, almost 20 years of developing software(including the past 8 with object-oriented languages) have shown me that OOPconcepts, when applied correctly, do in fact live up to their promise. Without furtherado, let's roll up our sleeves and see what all the fuss is about
Everything Is an Object
In a true object-oriented language, all problem domain entities are expressedthrough the concept of objects. (Note that in this book I'll be using the Coad/Yourdondefinition for "problem domain"-that is, that a problem domain is the problem you'reattempting to solve, in terms of its specific complexities, terminology, challenges,and so on.) As you might guess, objects are the central idea behind object-orientedprogramming. Most of us don't walk around thinking in terms of structures, datapackets, function calls, and pointers; instead, we typically think in terms of objects.Let's look at an example.If you were writing an invoicing application and you needed to tally the detail lines of the invoice, which of the following mental approaches would be more intuitive fromthe client's perspective? -Non-object-oriented approach I'll have access to a data structure representing aninvoice header. This invoice header structure will also include a doubly linked list of invoice detail structures, each of which contains a total line amount. Therefore, toget an invoice total, I need to declare a variable named something liketotalInvoiceAmount and initialize it to 0, get a pointer to the invoice headerstructure, get the head of the linked list of detail lines, and then traverse the linkedlist of detail lines. As I read each detail line structure, I'll get its member variablecontaining the total for that line and increment my totalInvoiceAmount variable.Object-oriented approach I'll have an invoice object, and I'll send a message to thatobject to ask it for the total amount. I don't need to think about how the informationis stored internally in the object, as I had to do with the non-object-oriented datastructure. I simply treat the object in a natural manner, making requests to it bysending messages. (The group of messages that an object can process arecollectively called the object's interface. In the following paragraph, I'll explain whythinking in terms of interface rather than implementation, as I have done here, is justifiable in the object-oriented approach.)Obviously, the object-oriented approach is more intuitive and closer to how many of us would think our way through a problem. In the second solution, the invoice objectprobably iterates through a collection of invoice detail objects, sending a message toeach one requesting its line amount. However, if what you're looking for is the total,you don't care how it's done. You don't care because one of the main tenets of object-oriented programming is encapsulation-the ability of an object to hide its
Leave a Comment