A well thought-out user interface of a class is key to a good design
of an object-oriented program.
Indeed, a poorly-designed interface that gives the user too much access
to implementation details can be just as risky as leaving data members public.
In this video we will compare two possible interfaces for a class.
In the first interface, the functionalities are too low-level
and reveal to the outside user
implementation details of the class.
While in the second interface,
only indispensable functionalities are provided.
We will see, of course, that the second option
is much better than the first one.
So we want to program a class that allows us to play
a game of tic-tac-toe. (TN: "jeu du morpion" means "tic-tac-toe")
As a reminder, it is a game for two players played on a 3 by 3 grid,
with the first player placing Os while the other player playing Xs.
The 2 players take turns and try to
get three of their pieces in a row
either across, diagonally, or vertically.
In this first version of the class "JeuMorpion",
the programmer has a minimalist vision of what
functionalities he offers to the outside world.
When he designed the class, he looked at what characteristics it had,
so he said to himself that logically he needed a grid,
then for the class functionalities, he provides the external user
a public initialization method to be
able to start with an empty grid in order to start playing.
He also provides a function
that gives access to the grid in order to place tokens
on it from outside the class,
such as red Os and blue Xs.
Let's examine more closely how the programmer
implemented this class JeuMorpion
and how he represented for example the grid.
Here, we see that he uses a one-dimensional array.
This is a possible, but perhaps not a very conventional choice.
So in this modelization, a grid is actually an object
with a reference
to a one-dimensional array.
So here, imagine for example
that we use the first 3 positions of the array for the first line,
the next 3, for the second line,
and the last 3 positions for the last line of the grid.