Prev | Current Page 79 | Next

Hasin Hayder

"Object-Oriented Programming with PHP5"

Strategy pattern is a common pattern helps
us make decisions on different cases, more easily. To understand this better, let us
use a scenario that you're developing a notifier program. This notifier program will
check the given options for a user. A user may want to be notified in many ways, like
email, SMS, or fax. Your program has to check the available options to contact
that user and then make a decision upon that. This case can easily be solved by
Strategy pattern:
Context
Strategy
SMS Notifier Email Notifier Fax Notifier
In the above pattern we are using three classes called SMSNotifier, EmailNotifier,
and FaxNotifier. All these classes implement the Notifier interface, which has a
method named notify. Each of these classes implement that method on their own.
Let's create the interface first.
//interface.Notifier.php
interface notifier
{
public function notify();
}
?>
Now we will create different types of notifiers.
class.emailnotifier.php
include_once("interface.notifier.php");
class EmailNotifier implements notifier
{
public function notify()
{
//do something to notify the user by Email
}
}
?>
Chapter 4
[ 69 ]
class.faxnotifier.


Pages:
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91