ÎåÖÖ³£¼ûµÄ PHP Éè¼Æģʽ(3)
Çåµ¥ 5 ÏÔʾÁË´ËģʽµÄÒ»¸öʾÀý¡£
Çåµ¥ 5. Chain.php
<?php interface ICommand { function onCommand( $name, $args ); } class CommandChain { private $_commands = array(); public function addCommand( $cmd ) { $this->_commands []= $cmd; } public function runCommand( $name, $args ) { foreach( $this->_commands as $cmd ) { if ( $cmd->onCommand( $name, $args ) ) return; } } } class UserCommand implements ICommand { public function onCommand( $name, $args ) { if ( $name != 'addUser' ) return false; echo( "UserCommand handling 'addUser'\n" ); return true; } } class MailCommand implements ICommand { public function onCommand( $name, $args ) { if ( $name != 'mail' ) return false; echo( "MailCommand handling 'mail'\n" ); return true; } } $cc = new CommandChain(); $cc->addCommand( new UserCommand() ); $cc->addCommand( new MailCommand() ); $cc->runCommand( 'addUser', null ); $cc->runCommand( 'mail', null ); ?>
´Ë´úÂ붨Òåά»¤ ICommand
¶ÔÏóÁбíµÄ CommandChain
Àà¡£Á½¸öÀ඼¿ÉÒÔʵÏÖ ICommand
½Ó¿Ú ¡ª¡ª Ò»¸ö¶ÔÓʼþµÄÇëÇó×÷³öÏìÓ¦£¬ÁíÒ»¸ö¶ÔÌí¼ÓÓû§×÷³öÏìÓ¦¡£ ͼ 5 ¸ø³öÁË UML¡£
ͼ 5. ÃüÁîÁ´¼°ÆäÏà¹ØÃüÁî
Èç¹ûÄúÔËÐаüº¬Ä³Ð©²âÊÔ´úÂëµÄ½Å±¾£¬Ôò»áµÃµ½ÒÔÏÂÊä³ö£º
% php chain.php UserCommand handling 'addUser' MailCommand handling 'mail' % |
´úÂëÊ×ÏÈ´´½¨ CommandChain
¶ÔÏ󣬲¢ÎªËüÌí¼ÓÁ½¸öÃüÁî¶ÔÏóµÄʵÀý¡£È»ºóÔËÐÐÁ½¸öÃüÁîÒԲ鿴˶ÔÕâЩÃüÁî×÷³öÁËÏìÓ¦¡£Èç¹ûÃüÁîµÄÃû³ÆÆ¥Åä UserCommand
»ò MailCommand
£¬Ôò´úÂëʧ°Ü£¬²»·¢ÉúÈκβÙ×÷¡£
Ϊ´¦ÀíÇëÇó¶ø´´½¨¿ÉÀ©Õ¹µÄ¼Ü¹¹Ê±£¬ÃüÁîÁ´Ä£Ê½ºÜÓмÛÖµ£¬Ê¹ÓÃËü¿ÉÒÔ½â¾öÐí¶àÎÊÌâ¡£
ÎÒÃǽ²ÊöµÄ×îºóÒ»¸öÉè¼ÆģʽÊDzßÂÔ Ä£Ê½¡£ÔÚ´ËģʽÖУ¬Ëã·¨ÊÇ´Ó¸´ÔÓÀàÌáÈ¡µÄ£¬Òò¶ø¿ÉÒÔ·½±ãµØÌæ»»¡£ÀýÈ磬Èç¹ûÒª¸ü¸ÄËÑË÷ÒýÇæÖÐÅÅÁÐÒ³µÄ·½·¨£¬Ôò²ßÂÔģʽÊÇÒ»¸ö²»´íµÄÑ¡Ôñ¡£Ë¼¿¼Ò»ÏÂËÑË÷ÒýÇæ µÄ¼¸¸ö²¿·Ö ¡ª¡ª Ò»²¿·Ö±éÀúÒ³Ã棬һ²¿·Ö¶ÔÿҳÅÅÁУ¬ÁíÒ»²¿·Ö»ùÓÚÅÅÁеĽá¹ûÅÅÐò¡£ÔÚ¸´ÔÓµÄʾÀýÖУ¬ÕâЩ²¿·Ö¶¼ÔÚͬһ¸öÀàÖС£Í¨¹ýʹÓòßÂÔģʽ£¬Äú¿É½«ÅÅÁв¿·Ö·ÅÈëÁíÒ»¸ö ÀàÖУ¬ÒÔ±ã¸ü¸ÄÒ³ÅÅÁеķ½Ê½£¬¶ø²»Ó°ÏìËÑË÷ÒýÇæµÄÆäÓà´úÂë¡£
×÷Ϊһ¸ö½Ï¼òµ¥µÄʾÀý£¬Çåµ¥ 6 ÏÔʾÁËÒ»¸öÓû§ÁбíÀ࣬ËüÌṩÁËÒ»¸ö¸ù¾ÝÒ»×é¼´²å¼´ÓõIJßÂÔ²éÕÒÒ»×éÓû§µÄ·½·¨¡£
Çåµ¥ 6. 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 ); ?>
´Ë´úÂëµÄ UML Èçͼ 6 Ëùʾ¡£
ͼ 6. Óû§ÁбíºÍÓÃÓÚÑ¡ÔñÓû§µÄ²ßÂÔ
UserList
ÀàÊÇ´ò°üÃû³ÆÊý×éµÄÒ»¸ö°ü×°Æ÷¡£ËüʵÏÖ find
·½·¨£¬¸Ã·½·¨ÀûÓü¸¸ö²ßÂÔÖ®Ò»À´Ñ¡ÔñÕâЩÃû³ÆµÄ×Ó¼¯¡£ÕâЩ²ßÂÔÓÉ IStrategy
½Ó¿Ú¶¨Ò壬¸Ã½Ó¿ÚÓÐÁ½¸öʵÏÖ£ºÒ»¸öËæ»úÑ¡ÔñÓû§£¬ÁíÒ»¸ö¸ù¾ÝÖ¸¶¨Ãû³ÆÑ¡ÔñÆäºóµÄËùÓÐÃû³Æ¡£ÔËÐвâÊÔ´úÂëʱ£¬½«µÃµ½ÒÔÏÂÊä³ö£º
% php strategy.php Array ( [0] => Jack [1] => Lori [2] => Megan ) Array ( [0] => Andy [1] => Megan ) % |
²âÊÔ´úÂëΪÁ½¸ö²ßÂÔÔËÐÐͬһÓû§ÁÐ±í£¬²¢ÏÔʾ½á¹û¡£ÔÚµÚÒ»ÖÖÇé¿öÖУ¬²ßÂÔ²éÕÒÅÅÁÐÔÚ J
ºóµÄÈκÎÃû³Æ£¬ËùÒÔÄú½«µÃµ½ Jack¡¢Lori ºÍ Megan¡£µÚ¶þ¸ö²ßÂÔËæ»úÑ¡È¡Ãû³Æ£¬Ã¿´Î»á²úÉú²»Í¬µÄ½á¹û¡£ÔÚÕâÖÖÇé¿öÏ£¬½á¹ûΪ Andy ºÍ Megan¡£
²ßÂÔģʽ·Ç³£Êʺϸ´ÔÓÊý¾Ý¹ÜÀíϵͳ»òÊý¾Ý´¦Àíϵͳ£¬¶þÕßÔÚÊý¾Ýɸѡ¡¢ËÑË÷»ò´¦ÀíµÄ·½Ê½·½ÃæÐèÒª½Ï¸ßµÄÁé»îÐÔ¡£
±¾ÎĽéÉܵĽö½öÊÇ PHP Ó¦ÓóÌÐòÖÐʹÓõļ¸ÖÖ×î³£¼ûµÄÉè¼Æģʽ¡£ÔÚÉè¼Æģʽ Ò»ÊéÖÐÑÝʾÁ˸ü¶àµÄÉè¼Æģʽ¡£²»ÒªÒò¼Ü¹¹µÄÉñÃØÐÔ¶ø·ÅÆú¡£Ä£Ê½ÊÇÒ»ÖÖ¾øÃîµÄÀíÄÊÊÓÃÓÚÈκαà³ÌÓïÑÔ¡¢Èκμ¼ÄÜˮƽ¡£
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ