Home  >  Article  >  Backend Development  >  PHP design pattern command chain pattern_PHP tutorial

PHP design pattern command chain pattern_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:54:201016browse

php design pattern command chain mode

Command chain mode:

The chain of command pattern is based on loosely coupled topics, sending messages, commands and requests, or sending arbitrary content through a set of handlers. Each handler makes its own judgment about whether it can handle the request. If it can, the request is processed and the process stops. You can add or remove handlers from the system without affecting other handlers.

1.interface Validator

2.{

3. /**

4. * The method could have any parameters.

5. * @param mixed

6. * @return boolean

7.*/

8. public function isValid($value);

9.}

10.

11./**

12. * ConcreteCommand.

13.*/

14.class MoreThanZeroValidator implements Validator

15.{

16. public function isValid($value)

17. {

18. return $value > 0;

19. }

20.}

21.

22./**

23. * ConcreteCommand.

24.*/

25.class EvenValidator implements Validator

26.{

27. public function isValid($value)

28. {

29. return $value % 2 == 0;

30. }

31.}

32.

33./**

34. * The Invoker. An implementation could store more than one

35. * Validator if needed.

36.*/

37.class ArrayProcessor

38.{

39. protected $_rule;

40.

41. public function __construct (Validator $rule)

42. {

43. $this->_rule = $rule;

44. }

45.

46. public function process(array $numbers)

47. {

48. foreach ($numbers as $n) {

49. if ($this->_rule->IsValid($n)) {

50. echo $n, "n";

51. }

52. }

53. }

54.}

55.

56.// Client code

57.$processor = new ArrayProcessor(new EvenValidator());

58.$processor->process(array(1, 20, 18, 5, 0, 31, 42));


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/998007.htmlTechArticlephp design pattern command chain mode Command chain mode: The command chain mode is based on loosely coupled themes, sending messages, Commands and requests, or sending arbitrary content through a set of handlers. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn