As a simpler example, Strategy.php shows a user list class that provides a method for finding a set of users based on a plug-and-play set of strategies.
Strategy.php
<?php interface IStrategy { function filter($record); } class FindAfterStrategy implements IStrategy { private $_name; public function __construct($name) { $this->_name = $name; } public function filter($record) { return strcmp($this->_name, $record) <= 0; } } class RandomStrategy implements IStrategy { public function filter($record) { return rand(0, 1) >= 0.5; } } class UserList { private $_list = array(); public function __construct($names) { if ($names != null) { foreach($names as $name) { $this->_list []= $name; } } } public function add($name) { $this->_list []= $name; } public function find($filter) { $recs = array(); foreach($this->_list as $user) { if ($filter->filter($user)) $recs []= $user; } return $recs; } } $ul = new UserList(array("Andy", "Jack", "Lori", "Megan")); $f1 = $ul->find( new FindAfterStrategy("J") ); print_r($f1); $f2 = $ul->find(new RandomStrategy()); print_r($f2); ?>
The user list and the strategies for selecting users
The
UserList
class is a wrapper around an array of names. It implements a find
method that takes one of several strategies for selecting a subset of those names. Those strategies are defined by the IStrategy
interface, which has two implementations: One chooses users randomly and the other chooses all the names after a specified name. When you run the test code, you get the following output:% php strategy.php Array ( [0] => Jack [1] => Lori [2] => Megan ) Array ( [0] => Andy [1] => Megan ) %
The test code runs the same user lists against two strategies and shows the results. In the first case, the strategy looks for any name that sorts after
J
, so you get Jack, Lori, and Megan. The second strategy picks names randomly and yields different results every time. In this case, the results are Andy and Megan.The strategy pattern is great for complex data-management systems or data-processing systems that need a lot of flexibility in how data is filtered, searched, or processed.
source: ibm.com
2 comments:
There are also ready to use Observer interface in SPL.
http://www.php.net/~helly/php/ext/spl/interfaceSplObserver.html
This pattern can also be very useful in the context of form validation.
Post a Comment