Hello to all.
This sequence will focus on class methods.
As we saw earlier,
class methods are normal methods, like other methods.
Generally, they are used to create new instances,
but can be used for other things.
We'll be seeing two examples of uses for class methods.
Imagine we want to parse these lines
in a computer program.
The lines create a structure.
If a line starts with an exclamation point,
it is read as a title.
Lines that start with dashes are list items.
Lines that start with more than one dash
are subitems on a list.
In any other case, it is a paragraph.
Take a program with this input:
It creates instances for the classes here,
subclasses of DocumentItem.
We also want to design this program
so that each subclass of DocumentItem is responsible
for parsing lines,
and for deciding "canParse" (or cannot) for a given line.
In the "canParse" method here, we submit a line of text.
If the line starts with an exclamation point,
the canParse method must return true.
The line belongs to the Section Title class.
In the ListItem class,
the canParse method returns true if the line starts with a dash.
And the canParse line we see here must always return true.
The idea is to write an algorithm that looks at classes one by one.
As soon as one returns canParse for a given line,
we use that class to create a new instance for that line.
That's the goal of canParse method.
The newFromLine method also takes a line of text as a parameter.
But it returns an instance in the class in which the line is implemented.
If I send a newFromLine message to the SectionTitle class,
with a chain of characters starting with an exclamation point,
the newFromLine method will return an instance of the SectionTitle class.
This is what happens:
The ClassParser does this work.
It searches DocumentItem subclasses
for the one in charge of a given line.
The Parser has a method called documentClasses.
Its goal is to return the list of Subclasses
in DocumentItem.
It returns a collection containing these three classes.
We do this by sending the message "allSubclasses" to DocumentItem.
The allSubclasses method already exists in the Pharo system.
It returns the list of subclasses in a given class.
That message yields a collection
containing SectionTitle, ListItem, and Text.
I want the collection sorted by priority.
The idea is for the text class to come at the end
because its canParse method always returns true.
The text class can parse any line at random.
We will test that method after testing the others.
We have the concept of priority here.
As you see, many methods are underlined.
If I underline the methods here,
this indicates they are class methods.
To execute these methods, the message must be sent to the Class.
I must send the canParse method to the SectionTitle class
to find out if the SectionTitle class can parse a given line.
The Parse method of the Parser class takes a line as a parameter.
It searches all DocumentItem subclasses for a class that can parse the line.
If it detects one, it creates an instance of this class.
Sending the command "detect:" to a collection
yields one among n elements in a collection, following a predicate.
Now we'll look at another example of using class methods.
We'll look at how command-line parameters are handled.
Here we see a program receiving parameters,
detecting the parameters that will make it do things differently.
The first parameter indicates the action to be carried out.
We will design the program so that we have one class per action type.
The "eval" action is represented by one class:
eval CommandLineHandler.
What action is carried out when the "eval" parameter appears?
It executes...
This chain of characters is interpreted as Pharo code,
and return the result of the expression.
3628800 is the result of 10 factorial.
This is implemented
using the Command-Line Handler class.
EvaluateCommandLineHandler is a subclass
assigned to processing the "eval" parameter.
If "eval" is sent as the first parameter in a command line,
this class has to work.
The isReponsibleFor method must return true
if there is one subclass
capable of taking the command line into account.
Underneath, we see
a mechanism identical to the one we saw in the preceding example.
Once again, we are using class methods and sending messages to classes
for tasks other than creating instances.
This sequence showed us two possible uses for class methods.
Class methods are mostly used to create new instances.
But they can also be used in other cases.