command chain mode:
The command chain pattern is based on loosely coupled topics, sending messages, commands, and requests, or sending arbitrary content through a set of handlers. Each handler will decide for itself whether it can handle the request. If it can, the request is processed and the process is stopped. You can add or remove handlers for your system without affecting other handlers.
1.interface Validator
0.9
3./**
4. * The method could have any parameters.
5. * @param mixed
6. * @return Boolean
7. */
8. Public Function IsValid ($value);
7.3
10.
11./**
* Concretecommand.
13. */
14.class Morethanzerovalidator implements Validator
15.{
Public Function IsValid ($value)
17. {
return $value > 0;
19.}
20.}
21st.
22./**
* Concretecommand.
24. */
25.class Evenvalidator implements Validator
26.{
Public Function IsValid ($value)
28. {
return $value% 2 = 0;
30.}
31.}
32.
33./**
* The invoker. An implementation could store more than one
* Validator if needed.
36. */
37.class Arrayprocessor
38.{
protected $_rule;
40.
Public function __construct (Validator $rule)
42. {
$this->_rule = $rule;
44.}
45.
Public function process (array $numbers)
47. {
foreach ($numbers as $n) {
if ($this->_rule->isvalid ($n)) {
echo $n, "\ n";
51.}
52.}
53.}
54.}
55.
56.//Client Code
$processor = new Arrayprocessor (new Evenvalidator ());
$processor->process (Array (1, 20, 18, 5, 0, 31, 42));