It is claimed that the problem-solving techniques used in object-oriented programming is very close to the way we day-to-day problems. This is why the use of Object Oriented techniques has become very useful and popular in modern day programming languages like Java, VC#.Net etc. Let's try to understand how Object Oriented programming is close to our daily life with the help of an example.
Suppose you wanted to send a gift to a friend named James who lives in another city.To solve this problem you simply walk to your nearest gift shop run by, lets say, Scott. You tell Scott the kind of gift you want to send to James, your budget, along with the address of James. Once you are done with these things, you can be sure that the gift will be delivered.
Suppose you wanted to send a gift to a friend named James who lives in another city.To solve this problem you simply walk to your nearest gift shop run by, lets say, Scott. You tell Scott the kind of gift you want to send to James, your budget, along with the address of James. Once you are done with these things, you can be sure that the gift will be delivered.
Now, lets examine the steps used to solve your problem.
• You first found an appropriate agent (Scott, in this example) and you passed to this agent a message containing a request.
• It is the responsibility of Scott to satisfy the request.
• There is some method (an algorithm or set of operations) used by Scott to do this.
• You do not need to know the particular methods used to satisfy the request—such information is hidden from view. Ofcourse, you do not want to know the details, but on investigation you may find that Scott delivered a slightly different message to another gift shop in the city where your friend James lives. That person then passes another message to a subordinate who makes the final arrangement.The gift, along with yet another message, is passed onto a delivery person and so on.
This leads to our first conceptual picture of object-oriented programming: An object-oriented program is structured as community of interacting agents called objects. Each object has a role to play. Each object provides a service or performs an action that is used by other members of the community. Messages and Responsibilities Members of an object-oriented community make requests of each other.
The next important principle explains the use of messages to initiate action:
The next important principle explains the use of messages to initiate action:
Action is initiated in object-oriented programming by the transmission of a message to an agent (an object) responsible for the actions. The message encodes the request for an action and is accompanied by any additional information (arguments/parameters) needed to carry out the request. The receiver is the object to whom the message is sent. If the receiver accepts the message, it accepts responsibility to carry out the indicated action. In response to a message, the receiver will perform some method to satisfy the request.
There are some important issues to point out here:
• The client sending the request need not know the means by which the request is carried out. In this we see the principle of information hiding.
• Another principle implicit in message passing is the idea of finding someone else to do the work i.e. reusing components that may have been written by someone else.
• The interpretation of the message is determined by the receiver and can vary with different receivers. For example, if you sent the message “deliver gift” to a friend, he will probably have understood what was required and gift would still have been delivered but the method he used would have been very different from that used by the person in the example.
• In object-oriented programming, behaviour is described in terms of responsibilities.
• Client’s requests for actions only indicates the desired outcome. The receivers are free to pursue any technique that achieves the desired outcomes.
• Thinking in this way allows greater independence between objects.
• Thus, objects have responsibilities that they are willing to fulfill on request. The collection of reponsibilities associated with an object is often called a protocol.
0 comments:
Post a Comment