WEBVTT

00:00:00.400 --> 00:00:01.600 align:middle
Hello to all.

00:00:01.760 --> 00:00:05.840 align:middle
This sequence
will focus on class methods.

00:00:06.320 --> 00:00:08.320 align:middle
As we saw earlier,

00:00:08.520 --> 00:00:12.840 align:middle
class methods are normal methods,
like other methods.

00:00:13.520 --> 00:00:17.440 align:middle
Generally, they are used
to create new instances,

00:00:17.760 --> 00:00:19.760 align:middle
but can be used for other things.

00:00:20.200 --> 00:00:24.080 align:middle
We'll be seeing two examples
of uses for class methods.

00:00:24.720 --> 00:00:28.280 align:middle
Imagine we want to parse
these lines

00:00:28.440 --> 00:00:30.840 align:middle
in a computer program.

00:00:32.120 --> 00:00:34.800 align:middle
The lines create a structure.

00:00:36.000 --> 00:00:39.720 align:middle
If a line starts
with an exclamation point,

00:00:40.120 --> 00:00:42.040 align:middle
it is read as a title.

00:00:42.480 --> 00:00:47.720 align:middle
Lines that start with dashes
are list items.

00:00:49.240 --> 00:00:51.920 align:middle
Lines that start
with more than one dash

00:00:52.120 --> 00:00:54.760 align:middle
are subitems on a list.

00:00:55.680 --> 00:00:58.000 align:middle
In any other case, it is a paragraph.

00:00:58.800 --> 00:01:01.560 align:middle
Take a program with this input:

00:01:03.080 --> 00:01:05.640 align:middle
It creates instances
for the classes here,

00:01:05.800 --> 00:01:07.960 align:middle
subclasses of DocumentItem.

00:01:10.080 --> 00:01:13.080 align:middle
We also want to design
this program

00:01:13.280 --> 00:01:16.600 align:middle
so that each subclass
of DocumentItem is responsible

00:01:16.760 --> 00:01:17.880 align:middle
for parsing lines,

00:01:18.040 --> 00:01:21.640 align:middle
and for deciding "canParse" (or cannot)
for a given line.

00:01:21.800 --> 00:01:26.680 align:middle
In the "canParse" method here,
we submit a line of text.

00:01:27.000 --> 00:01:30.440 align:middle
If the line starts
with an exclamation point,

00:01:30.600 --> 00:01:32.920 align:middle
the canParse method must return true.

00:01:33.080 --> 00:01:35.840 align:middle
The line belongs
to the Section Title class.

00:01:36.440 --> 00:01:37.920 align:middle
In the ListItem class,

00:01:38.640 --> 00:01:43.920 align:middle
the canParse method returns true
if the line starts with a dash.

00:01:44.760 --> 00:01:48.400 align:middle
And the canParse line we see here
must always return true.

00:01:49.240 --> 00:01:54.320 align:middle
The idea is to write an algorithm
that looks at classes one by one.

00:01:54.920 --> 00:01:58.120 align:middle
As soon as one returns
canParse for a given line,

00:01:58.840 --> 00:02:03.480 align:middle
we use that class to create
a new instance for that line.

00:02:04.600 --> 00:02:06.840 align:middle
That's the goal of canParse method.

00:02:07.000 --> 00:02:11.840 align:middle
The newFromLine method also
takes a line of text as a parameter.

00:02:12.960 --> 00:02:17.560 align:middle
But it returns an instance in the class
in which the line is implemented.

00:02:17.920 --> 00:02:22.920 align:middle
If I send a newFromLine message
to the SectionTitle class,

00:02:23.200 --> 00:02:27.120 align:middle
with a chain of characters
starting with an exclamation point,

00:02:27.520 --> 00:02:31.880 align:middle
the newFromLine method will return
an instance of the SectionTitle class.

00:02:32.360 --> 00:02:33.760 align:middle
This is what happens:

00:02:34.080 --> 00:02:36.880 align:middle
The ClassParser does this work.

00:02:37.040 --> 00:02:39.680 align:middle
It searches
DocumentItem subclasses

00:02:40.000 --> 00:02:42.160 align:middle
for the one
in charge of a given line.

00:02:42.520 --> 00:02:46.360 align:middle
The Parser has a method
called documentClasses.

00:02:47.320 --> 00:02:51.720 align:middle
Its goal is to return
the list of Subclasses

00:02:52.160 --> 00:02:53.520 align:middle
in DocumentItem.

00:02:53.840 --> 00:02:58.040 align:middle
It returns a collection
containing these three classes.

00:02:58.640 --> 00:03:02.320 align:middle
We do this by sending the message
"allSubclasses" to DocumentItem.

00:03:02.480 --> 00:03:05.800 align:middle
The allSubclasses method
already exists in the Pharo system.

00:03:05.960 --> 00:03:09.160 align:middle
It returns the list of subclasses
in a given class.

00:03:09.640 --> 00:03:13.000 align:middle
That message yields a collection

00:03:13.160 --> 00:03:16.440 align:middle
containing SectionTitle,
ListItem, and Text.

00:03:17.960 --> 00:03:20.520 align:middle
I want the collection
sorted by priority.

00:03:20.680 --> 00:03:24.400 align:middle
The idea is for the text class
to come at the end

00:03:24.680 --> 00:03:27.960 align:middle
because its canParse method
always returns true.

00:03:28.120 --> 00:03:31.640 align:middle
The text class
can parse any line at random.

00:03:32.960 --> 00:03:36.080 align:middle
We will test that method
after testing the others.

00:03:36.240 --> 00:03:39.520 align:middle
We have the concept
of priority here.

00:03:41.160 --> 00:03:44.960 align:middle
As you see, many methods
are underlined.

00:03:45.240 --> 00:03:48.760 align:middle
If I underline the methods here,

00:03:49.160 --> 00:03:51.720 align:middle
this indicates they are class methods.

00:03:52.040 --> 00:03:56.240 align:middle
To execute these methods,
the message must be sent to the Class.

00:03:56.480 --> 00:04:01.400 align:middle
I must send the canParse method
to the SectionTitle class

00:04:01.760 --> 00:04:05.840 align:middle
to find out if the SectionTitle class
can parse a given line.

00:04:08.240 --> 00:04:12.560 align:middle
The Parse method of the Parser class
takes a line as a parameter.

00:04:12.960 --> 00:04:18.760 align:middle
It searches all DocumentItem subclasses
for a class that can parse the line.

00:04:19.040 --> 00:04:22.600 align:middle
If it detects one, it creates an instance
of this class.

00:04:24.200 --> 00:04:28.600 align:middle
Sending the command "detect:"
to a collection

00:04:29.160 --> 00:04:33.520 align:middle
yields one among n elements
in a collection, following a predicate.

00:04:34.400 --> 00:04:38.920 align:middle
Now we'll look at another example
of using class methods.

00:04:39.320 --> 00:04:43.800 align:middle
We'll look at how command-line
parameters are handled.

00:04:44.480 --> 00:04:48.160 align:middle
Here we see a program
receiving parameters,

00:04:49.040 --> 00:04:52.960 align:middle
detecting the parameters
that will make it do things differently.

00:04:53.360 --> 00:04:57.160 align:middle
The first parameter indicates
the action to be carried out.

00:04:57.680 --> 00:05:02.080 align:middle
We will design the program so that
we have one class per action type.

00:05:02.400 --> 00:05:06.000 align:middle
The "eval" action is represented
by one class:

00:05:06.360 --> 00:05:08.160 align:middle
eval CommandLineHandler.

00:05:09.920 --> 00:05:13.800 align:middle
What action is carried out
when the "eval" parameter appears?

00:05:14.440 --> 00:05:17.000 align:middle
It executes...

00:05:17.280 --> 00:05:21.120 align:middle
This chain of characters
is interpreted as Pharo code,

00:05:21.560 --> 00:05:24.040 align:middle
and return the result
of the expression.

00:05:24.680 --> 00:05:27.440 align:middle
3628800 is the result
of 10 factorial.

00:05:27.920 --> 00:05:29.440 align:middle
This is implemented

00:05:30.360 --> 00:05:32.920 align:middle
using the Command-Line Handler
class.

00:05:33.960 --> 00:05:36.440 align:middle
EvaluateCommandLineHandler
is a subclass

00:05:36.920 --> 00:05:41.480 align:middle
assigned to processing
the "eval" parameter.

00:05:41.800 --> 00:05:45.560 align:middle
If "eval" is sent as the first parameter
in a command line,

00:05:45.720 --> 00:05:47.560 align:middle
this class has to work.

00:05:48.680 --> 00:05:52.560 align:middle
The isReponsibleFor method
must return true

00:05:53.200 --> 00:05:54.960 align:middle
if there is one subclass

00:05:55.120 --> 00:05:59.920 align:middle
capable of taking the command line
into account.

00:06:01.400 --> 00:06:02.760 align:middle
Underneath, we see

00:06:03.240 --> 00:06:07.640 align:middle
a mechanism identical to the one
we saw in the preceding example.

00:06:08.080 --> 00:06:13.160 align:middle
Once again, we are using class methods
and sending messages to classes

00:06:14.400 --> 00:06:16.800 align:middle
for tasks other than creating instances.

00:06:17.600 --> 00:06:21.480 align:middle
This sequence showed us
two possible uses for class methods.

00:06:22.200 --> 00:06:26.360 align:middle
Class methods
are mostly used to create new instances.

00:06:26.680 --> 00:06:29.320 align:middle
But they can also be used
in other cases.

