> php教程 > php手册 > 본문

php设计模式之命令链模式

WBOY
풀어 주다: 2016-06-13 09:04:58
원래의
959명이 탐색했습니다.

php设计模式之命令链模式

命令链模式:

   命令链模式以松散耦合主题为基础,发送消息、命令和请求,或通过一组处理程序发送任意内容。每个处理程序都会自行判断自己能否处理请求。如果可以,该请求被处理,进程停止。您可以为系统添加或移除处理程序,而不影响其他处理程序。

 

 

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));

 

 

 

 

 


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!