PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 11 ■ PERFORMING AND REPRESENTING TASKS


Figure 11–5. Extracting algorithms into their own type


Here are the Question classes rendered as code:

abstract class Question {
protected $prompt;
protected $marker;


function __construct( $prompt, Marker $marker ) {
$this->marker=$marker;
$this->prompt = $prompt;
}


function mark( $response ) {
return $this->marker->mark( $response );
}
}


class TextQuestion extends Question {
// do text question specific things
}


class AVQuestion extends Question {
// do audiovisual question specific things
}


As you can see, I have left the exact nature of the difference between TextQuestion and AVQuestion to
the imagination. The Question base class provides all the real functionality, storing a prompt property
and a Marker object. When Question::mark() is called with a response from the end user, the method
simply delegates the problem solving to its Marker object.
Now to define some simple Marker objects:


abstract class Marker {
protected $test;


function __construct( $test ) {

Free download pdf