Command chain model of PHP design pattern

Source: Internet
Author: User
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));




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.