Why Do We Care About OOP?

 <  Day Day Up  >  

This entire book focuses on developing object-oriented applications. This often brings out the question, "Why should I build this application using OOP, instead of the traditional way I've built applications for years ?" Unfortunately, there is no one easy answer to this question, but like most business decisions, it becomes a matter of time and money.

An object-oriented system acts as a scale model of the business entity for which it was built. By designing the system this way, the system can be adaptive to the business it was built to serve.

Studies have repeatedly shown that 70 “80 percent of all costs throughout the lifecycle of a software project come after the software has been launched. Specifically , these costs result from maintenance and changes to the software. Object-oriented systems by their nature are more adaptable to change and therefore can reduce these costs significantly.

The three key benefits of OOP are extensibility, reusability, and adaptability:

  • Extensibility. An object designed with this in mind is more likely to be reusable in other applications for that business; for example, a product company with an object modeled around their products can likely use that object in all their applications. Additionally, these objects are more likely to be adaptable or extensible to other needs for that same business.

  • Reusability. If an object can be reused from other projects, it doesn't need to be built or tested again. This saves time and money.

  • Adaptability. An object that is easily adaptable in new and originally unforeseen situations mitigates the need for new development, also saving time and money.

The true differentiations between procedural programming and OOP are three key concepts known as encapsulation, inheritance, and delegation.


The most basic concept of object-oriented design is encapsulation. Encapsulation is nothing more than designing a system with objects that contain all the data of an entity in the system (properties) as well as all the actions that can be done to or by the entity ( methods ). It is this encapsulation that is the core difference between OOP and procedural programming.

The procedural world uses a series of independent functions to achieve its goals, and each function requires that the data on which it operates be passed to it with each invocation. In a well-designed, object-oriented system, these functions are encapsulated into objects as methods. The objects with these methods are self-aware, meaning that the data of the object does not need to be passed into the method.

In Chapter 7, we examine the idea of messages, which is how objects talk to each other. You will see that in inter-object communication, data is still passed into the methods, but methods operating on the objects to which they belong have no such need.


As you model objects based on the real-world objects they mimic , you find relationships between objects that can be defined by the model "x is a y." One example of this is "a triangle is a shape." In the object-oriented world, you could implement this type of relationship with inheritance. Thus, any properties and methods of the Shape class would be inherited by the Triangle class.

Let's look deeper into this example. Triangles come in many varieties: isosceles (where two sides have equal lengths), equilateral (where all three sides have equal lengths), and right (where two sides meet at a 90-degree angle). Of course, there are also triangles that meet none of these conditions.

It is safe to say that all four types of triangles share the properties and methods of a triangle. What are the properties of a triangle? Every triangle has a number of sides (always three, by definition). They also have a height and width (often referred to as a "base" for triangles). How about methods? What can be done to, or with, a triangle? A simple method available for all triangles is the ability to calculate the area. Listing I.1 shows a simple Triangle class.

Listing I.1. A Simple Triangle Class Definiti on
 class Triangle{       var sides:Number=3;       var height:Number;       var base:Number;       function calculateArea():Number{             return .5*height*base;       } } 


Don't worry about the specific syntax here; class definition is covered in detail in Chapter 2.

This code defines a Triangle class that has three properties ( sides , height , and base ) and one method ( calculateArea ). Using inheritance, you could build a subclass that takes this class as a foundation and builds on it. Equilateral , Isosceles , and RightTriangle classes could all be created as subclasses of Triangle . They would all have access to the properties and methods defined here for the Triangle class, without the need to cut and paste this definition into each of them.


Inheritance is covered in detail in Chapter 5.

The ability to extend an existing class into a new class using inheritance is one of the key reasons object-oriented applications are as adaptable as they are. The core classes do not need to be redesigned to cover every possibility. Instead, they are designed to cover the details common across all instances, and subclasses are used to cover the differences.


Polymorphism is a way of describing something that can take many forms, meaning that it can react differently in different contexts. Chapter 5 delves into the concepts of polymorphism and how it applies to development.

In a polymorphic system, the same command can be interpreted by different objects independently. In the real-world example of driving a car, the driver sends a message to drive the car by putting the car in gear and pressing the gas pedal. How this message is interpreted by the car will vary greatly, based on whether it is a gas or electric engine and whether it is carbureted or fuel injected. However, the end result is always the same. The great benefit of polymorphism is that we need not understand how the object will interpret the command; we can simply tell the object what we want it to do and then let it be done.

In the object-oriented world, we can use polymorphism in a similar fashion. A subclass can use the methods of its parent class or override those methods in its own implementation. Therefore, it can always be known that the class will have a method with the same name as the method of the superclass, but we do not need to know specifically how that subclass will implement its method.

 <  Day Day Up  >  

Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net