Saturday, December 26, 2009

The Main Benefits of OOP

Encapsulation
Encapsulation is probably the feature that programmers appreciate most in object-oriented programming. In a nutshell, an object is the sole owner of its own data. All data is stored inside a memory area that can’t be directly accessed by another portion of the application, and all assignment and retrieval operations are performed through methods and properties provided by the object itself. This simple concept has at least two far-reaching consequences:
• You can check all the values assigned to object properties before they’re actually stored in memory and immediately reject all invalid ones.
• You’re free to change the internal implementation of the data stored in an object without changing the way the rest of the program interacts with the object. This means that you can later modify and improve the internal workings of a class without changing a single line of code elsewhere in the application.
As with most OOP features, it’s your responsibility to ensure that the class is well encapsulated.
The second goal that every programmer should pursue is code reusability, which you achieve by creating classes that are easily maintained and reused in other applications. This is a key factor in reducing the development time and cost. Classes offer much in this respect, but again they require your cooperation. When you start writing a new class, you should always ask yourself: Is there any chance that this class can be useful in other applications? How can I make this class as independent as possible from the particular software I’m developing right now? In most cases, this means adding a few additional properties or additional arguments to methods, but the effort often pays off nicely. Don’t forget that you can always resort to default values for properties and optional arguments for methods, so in most cases these enhancements won’t really make the code that uses the class more complex than it actually needs to be.
The concept of self-containment is also strictly related to code reuse and encapsulation. If you want to create a class module that’s easily reusable, you absolutely must not allow that class to depend on any entity outside it, such as a global variable. This would break encapsulation (because code elsewhere in the application might change the value of the variable to some invalid data) and above all, it would prevent you from reusing the class elsewhere without also copying the global variable (and its parent BAS module). For the same reason, you should try to make the class independent of general-purpose routines located in another module.
Polymorphism
Informally, Polymorphism is the ability of different classes to expose similar (or identical) interfaces to the outside. The most evident kind of polymorphism in Visual Basic is forms and controls. TextBox and PictureBox controls are completely different objects, but they have some properties and methods in common, such as Left, BackColor, and Move. This similarity simplifies your job as a programmer because you don’t have to remember hundreds of different names and syntax formats. More important, it lets you manage a group of controls using a single variable (typed as Control, Variant, or Object) and create generic procedures that act on all the controls on a form and therefore noticeably reduce the amount of code you have to write.
Inheritance
Inheritance is the ability, offered by many OOP languages, to derive a new class (the derived or inherited class) from another class (the base class). The derived class automatically inherits the properties and methods of the base class. For example, you could define a generic Shape class with properties such as Color and Position and then use it as a base for more specific classes (for example, Rectangle, Circle, and so on) that inherit all those generic properties. You could then add specific members, such as Width and Height for the Rectangle class and Radius for the Circle class. It’s interesting to note that, while polymorphism tends to reduce the amount of code necessary to use the class, inheritance reduces the code inside the class itself and therefore simplifies the job of the class creator. Unfortunately, Visual Basic doesn’t support inheritance, at least not in its more mature form of implementation inheritance.

No comments:

Post a Comment