Monday, January 14, 2008

The Power of Reuse

The Power of Reuse


One of the most important features of object-oriented programming is the ability to modify existing solution to solve new problems. If a particular kind of problem has been solved using the OOP approach, a similar but slightly different problem can usually be solved by making some changes in the object-message protocal that already exist. Most of the time, this requires adding new messages. Other cases may require adding new objects and new messages to which those objects respond.

Reusability is probably the most important and the strongest feature of Smalltalk. Although procedural languages can be reuse too, Smalltalk makes programming for reuse much easier.

Inheritance

Inheritance


Another important concept of object-oriented programming is inheritance. Inheritance allows a class to have the same behavior as another class and extend or tailor that behavior to provide special action for specific needs.

Let's use the following application as an example. Both Graduate class and Undergraduate class have similar bahavior such as managing a name, an address, a major, and a GPA. Rather than put this bahavior in both of these classes, the bahavior is placed in a new class called Student. Both Graduate and Undergraduate become subclass of the Student class, and both inherit the Student behavior.

Both Graduate and Undergraduate classes can then add additional behavior that is unique to them. For example, Graduate can be either Master's program or phD program. On the other hand, Undergraduate class might want to keep track of either the student is Freshman, Sophmore, Junior or Senior.

Classes that inherit from a class are called subclasses. The class a subclass inherits from are called superclass. In the example, Student is a superclass for Graduate and Undergraduate. Graduate and Undergraduate are subclasses of Student.

Polymorphism

Polymorphism


Another benefit of separating implementaion from behavior is polymorphism. Polymorphism allows two or more objects respond to the same message. A method called name could also be implemented for an object of the class Course. Even though the implementation of this name message may return a course number and a course title, its protocol is the same as the name message to the Student object. Polymorphism allows a sending object to communicate with different objects in a consistant manner without worrying about how many different implementations of a message.

An analogy of polymorphism to daily life is how students response to a school bell. Every student knows the significant of the bell. When the bell (message) rings, however, it has its own meaning to different students (objects). Some students go home, some go to the library, and some go to other classes. Every student responds to the bell, but how they response to it might be different.

Another example of polymorphism is the function of printing. Every printable object must know how to print itself. The message is the same to all the different objects: print, but the actual implementation of what they must do to print themselves varies.

The sending object does not have to know how the receiving object implement the message. Only the receiving objects worries about that. Assume that there is a printPage method in a Document object that has the responsibility of printing a page. To print the page, the printPage method sends the print message to each object on the page. The Document does not need to know what types of objects are on the page, only that each object supports the behavior of printing.

New objects can be added to the page without affecting the printPage method. This method still sends the print message and the new object provides its own print method in response to that message.

Polymorphism allows the sending object to communicate with receiving objects without having to understand what type of object it is, as long as the receiving objects support the messages.

Encapsulation

Encapsulation

In object-oriented programming, objects interact with each other by messages. The only thing that an object knows about another object is the object's interface. Each object's data and logic is hidden from other objects. In other words, the interface encapsulates the object's code and data.


This allow the developer to separate an object's implementation from its bahavior. This separation creates a "black-box" affect where the user is isolated from implementation changes. As long as the interface remains the same, any changes to the internal implementation is transparent to the user. For example, if the name message is sent to the Student object, it does not matter to the user how the developer implemented the code to handle this message. All the sending object needs is the correct protocol for interacting with the Student object. The developer can change the implementation at any time, but the name message would still work because the interface is the same.

Object-Oriented Paradigm
A computer language is object-oriented if they support the four specific object properties called abstraction, polymorphism, inheritance, and encapsulation.

Inheritance??!!
Data Abstraction???
Encapsulation?!!?
Reuse???
Polymorphism??
Smalltalk??!!

The objective of this section is to provide a thorough understanding of the principles of object-oriented paradigm.

Object-Oriented Problem Solving Approach

Object-oriented problem solving approach is very similar to the way a human solves daily problems. It consists of identifying objects and how to use these objects in the correct sequence to solve the problem. In other words, object-oriented problem solving can consist of designing objects whose behavior solves a specific problem. A message to an object causes it to perform its operations and solve its part of the problem.

The object-oriented problem solving approach, in general, can be devided into four steps. They are:

  1. Identify the problem,
  2. Identify the objects needed for the solution,
  3. Identify messages to be sent to the objects, and
  4. Create a sequence of messages to the objects that solve the problem.

Following is an example of solution to a simple problem using the four steps.

Example 1: Object-oriented Problem Solving Approach

Step 1: Problem Identification - Compute the sum of two numbers and print out the result.
Step 2: Object Identification - Identify objects needed to solve the problem.
Num1 - first number (object).
Num2 - second number (object).
Sum - result of the addition of the two numbers (object).
Step 3: Message Identification - Messages needed to send to objects.

+ aNum - This is the message sent to the receiver, object with an parameter aNum. The result of this message is the value (a numeric object) of the total sum of the receiver object and aNum.
print - a message that displays the value of the receiver
object.

Step 4: Object-message sequences - Following is the object-message sequence to solve the problem.
(Num1 + Num2)print

The message + with a parameter Num2 (an object), is sent to the object Num1. The result of this is an object (Num1 + Num2), which is sent the message print. The parentheses are included to avoid any ambiguity as to which message should be activated first.

Note: The sequence of Smalltalk evaluation is from left to right unless is specified by parentheses ( ).

For example:
2 + 3 * 4 is 20
while 2 +(3 * 4) is 14

Object's Data

Object's Data

Each object need to keep the information on how to perform its defined behavior. Some objects also contain variables that support their bahavior. These variables are called instance variables. Only the instance method for an object can refer to and change the values stored in the instance variables. The instance methods for other objects cannot refer to this object's data. An object may only access another object's data by sending it messages.This is called encapsulation and assures that there is a secure process for getting to an object's data.

Refer to the following diagram, the instance variables variableOne through variableX can only be accessed by the sender via instance methods methodOne through methodX. The sender can not refer to the variables directly by itslef.


Unlike procedural progamming where common data areas are often used for sharing information, object-oriented programming discourages direct access to common data (other than the use of global variables) by other programs. Only the object that "owns" the data can change its contant. Other objects can view or change this data by sending message to the "owner."

The instance variable names can be identical to the method names that associate with them. For example, the Student object has methods of name, address, and major as well as instance variables of name, address, and major. Smalltalk distinguishes variable identifier and a method identifier by the identifier's position in the expression.

 

Tamil Video Songs | Make Money Online