Class Methods At Work

Damien Cassou, St├ęphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

What You Will Learn

Parsing Lines

Imagine we want to parse

!Section Title
- list item
-- subitem

Any text here

A Possible Design

Parsing Lines

Parser >> documentClasses
   ^ DocumentItem allSubclasses
      sorted: [ :class1 :class2 | class1 priority < class2 priority ]

Parser >> parse: line
   self documentClasses
      detect: [ :subclass |
         (subclass canParse: aLine)
            ifTrue: [ ^ subclass newFromLine: line ] ]

The Command-Line Handler

$ pharo Pharo.image eval "10 factorial"
3628800

The Command-Line Handler

CommandLineHandler class >> isResponsibleFor: arguments
   ^ arguments includesSubCommand: self commandName

EvaluateCommandLineHandler class >> commandName
   ^ 'eval'

CommandLineHandler class >> allHandlers
   ^ self allSubclasses
      reject: [ :handler| handler isAbstract ]

CommandLineHandler class >> handlersFor: arguments
   ^ self allHandlers
      select: [ :handlerClass |
         handlerClass isResponsibleFor: arguments ]

Conclusion

/