PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 11 ■ PERFORMING AND REPRESENTING TASKS

// send mail to sysadmin
print CLASS.":\tsending mail to sysadmin\n";
}
}
}


class GeneralLogger extends LoginObserver {
function doUpdate( Login $login ) {
$status = $login->getStatus();
// add login data to log
print CLASS.":\tadd login data to log\n";
}
}


class PartnershipTool extends LoginObserver {
function doUpdate( Login $login ) {
$status = $login->getStatus();
// check IP address
// set cookie if it matches a list
print CLASS.":\tset cookie if IP matches a list\n";
}
}


Creating and attaching LoginObserver classes is now achieved in one go at the time of instantiation:

$login = new Login();
new SecurityMonitor( $login );
new GeneralLogger( $login );
new PartnershipTool( $login );


So now I have created a flexible association between the subject classes and the observers. You can
see the class diagram for the example in Figure 11–6.

Free download pdf