Class Methods At Work
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
What You Will Learn
- Class methods are normal methods
- Most class methods create new instances
- but they can be used for other things
Parsing Lines
Imagine we want to parse
A Possible Design
- Document item classes know
- if they can parse a line (
canParse:
)
- how to create instances (
newFromLine:
)
Parsing Lines
The Command-Line Handler
- the Pharo command-line interface (CLI) uses the same approach
- each subclass of
CommandLineHandler
knows how to deal with one command
- the correct subclass is selected by sending messages to the class
The Command-Line Handler
Conclusion
- Classes are objects and can be sent messages
- Method lookup is exactly the same as for all objects:
- go to the class of the receiver
- follow inheritance chain
- More during the lecture Understanding Metaclasses
- Pharo makes it easy to iterate over subclasses
/