-In this lecture, I will teach you what an object is.
In fact, an object is a small entity that has a state
and reacts to messages by executing methods.
So, what is an object?
An object is a small computer entity characterized by three properties.
It has a state, a behavior and an identity,
and it will collaborate with other objects,
because it does not live in isolation, to perform more complex tasks.
I will start with a few examples.
Imagine a really simple object, such as a die.
The state of a die is its number of faces.
Its behavior is: "I want to roll the die to get a result."
And this die will probably live in "aDieHandle",
so that we can roll several dice together.
Now, if we take the example of a rectangle...
A rectangle is characterized by an X and a Y,
and an extent, or a width and a height.
That is the state of the rectangle.
Then, the rectangle knows how to clip itself,
what its area is, how to expand, shrink,
and all these kinds of behavior.
In fact, a rectangle also interacts with other objects.
For example, the canvas on your computer
will have rectangles to minimize the redisplay of your window.
So, when you move your window, the display will generate rectangles
and compute new rectangles to redisplay what should be redisplayed.
Now, here is the example of a contact.
In your iPhone, you have a friend as a contact.
So, this is an object that will represent his or her e-mail address, name,
address, phone number and picture.
The behavior will be that you can display this object
or query it.
And also, this contact will work in a contact manager.
So, the contact manager will be able to manage some of these objects.
Similarly, if you collect games, for example,
and you want to know what games you have,
you will have a game item which has a name.
Is it on PS2 or PS3?
How much did it cost?
Did you finish it or not?
And then, you will be able to serialize this object on a disk,
and it will collaborate with an item-collector system
that will help you collect your games.
So, what you see is that an object has a state,
a behavior and an identity, and will collaborate with other objects
to perform more complex tasks.
I mentioned the identity of the object.
Often, you confuse identity and equality.
Here is a trick to avoid confusion.
You enter a pizzeria and want to order a pizza.
You want to order the same pizza as a guy.
In the case of identity, you would eat his pizza.
In the case of equality, you only want the same.
So, identity is the real object that you point with your finger,
whereas equality means wanting the same.
Are those objects the same?
In fact, an object has a responsibility,
and it will expose this responsibility to you
via a behavior, by executing a behavior.
For example, a turtle has a responsibility
to draw a line when it moves.
This is the responsibility of this object.
A file, for example, has a responsibility to have a content and to open itself.
It is really important to understand that objects have responsibilities.
And these responsibilities are embedded and realized
by the behavior that is given when it is executed.
An object will also encapsulate data.
So, what you can see on the slide
is that we can think that data is not exposed to the client.
So, data is something that only the method of the object can access.
So, we should consider data as something private.
The methods are exposed, and you, as a client,
or other objects, as clients, can only send messages to the object
to execute the methods that will modify or not,
or use the private data of the object.
That is why, on this slide, I have this larger border
between messages and methods.
An object will react to messages.
For example, I send the message "go" to the turtle
and as a reaction, the turtle will execute a method
that will move the turtle on the screen and draw a line on the screen.
Now, what you should see is that different objects
can understand the same message.
In fact, I can ask a window to open and get a new window on my desktop,
or I can ask a window to open a file, and I will look at its content,
or if I open a Zip archive, I want to see the Zip item inside.
And what you see is that it is the same message,
but I have three different behaviors that have been executed.
Now, a method specifies a behavior.
So, you have the class "rectangle" that defines the height of a rectangle.
To do so, it will compute, for example,
the difference between the bottom and the top point of the rectangle.
Now, a rectangle can also define a method "area"
to compute its area by multiplying the width by the height.
The methods specify the behavior that will be executed
in reaction to the message.
Now, a method can also send other messages to itself.
For example, the message "topCenter" will send the message "center"
and the message "top".
The message "center" will also send the message "topLeft" and "bottomRight"
to the rectangle.
This means that we will have a cascade of messages
in reaction to the execution of a method.
And there is no problem.
What I show in this example is that all the methods
were methods of the class "rectangle".
We will see what a class is later.
But in fact, it does not have to be this way.
This means that an object can also delegate to other objects.
So, an object can say: "To perform this task,
I will send messages to other objects and ask them to perform subtasks.
And then, I will take the result and recombine it
to build something larger."
Here is a typical example of delegation.
An item collector can ask its items to save themselves on the disk.
Or it can say: "I will ask another object
that is responsible for serializing objects
to serialize myself."
It depends. This is a design choice to know how we distribute
the responsibility in the system.
What is important to realize is that you, as an object,
can delegate a task to somebody else.
What you should know is that an object is characterized by a state,
a behavior and an identity,
that an object collaborates with other objects to realize tasks,
that an object answers to messages and executes methods by reaction,
and that an object may delegate a computation to another object,
or to several other objects,
and combine them to perform a task.