ÎåÖÖ³£¼ûµÄ PHP Éè¼Æģʽ(2)
Çåµ¥ 3 ÖеĴúÂëÏÔʾÁË PHP V5 ÖеÄÒ»¸öÊý¾Ý¿âÁ¬½Óµ¥ÔªËØ¡£
Çåµ¥ 3. Singleton.php
<?php require_once("DB.php"); class DatabaseConnection { public static function get() { static $db = null; if ( $db == null ) $db = new DatabaseConnection(); return $db; } private $_handle = null; private function __construct() { $dsn = 'mysql://root:password@localhost/photos'; $this->_handle =& DB::Connect( $dsn, array() ); } public function handle() { return $this->_handle; } } print( "Handle = ".DatabaseConnection::get()->handle()."\n" ); print( "Handle = ".DatabaseConnection::get()->handle()."\n" ); ?>
´Ë´úÂëÏÔʾÃûΪ DatabaseConnection
µÄµ¥¸öÀà¡£Äú²»ÄÜ´´½¨×ÔÒÑµÄ DatabaseConnection
£¬ÒòΪ¹¹Ô캯ÊýÊÇרÓõġ£µ«Ê¹Óþ²Ì¬ get
·½·¨£¬Äú¿ÉÒÔ»ñµÃÇÒ½ö»ñµÃÒ»¸ö DatabaseConnection
¶ÔÏó¡£´Ë´úÂëµÄ UML Èçͼ 3 Ëùʾ¡£
ͼ 3. Êý¾Ý¿âÁ¬½Óµ¥ÔªËØ
ÔÚÁ½´Îµ÷Óü䣬handle
·½·¨·µ»ØµÄÊý¾Ý¿â¾ä±úÊÇÏàͬµÄ£¬Õâ¾ÍÊÇ×îºÃµÄÖ¤Ã÷¡£Äú¿ÉÒÔÔÚÃüÁîÐÐÖÐÔËÐдúÂëÀ´¹Û²ìÕâÒ»µã¡£
% php singleton.php Handle = Object id #3 Handle = Object id #3 % |
·µ»ØµÄÁ½¸ö¾ä±úÊÇͬһ¶ÔÏó¡£Èç¹ûÄúÔÚÕû¸öÓ¦ÓóÌÐòÖÐʹÓÃÊý¾Ý¿âÁ¬½Óµ¥ÔªËØ£¬ÄÇô¾Í¿ÉÒÔÔÚÈκεط½ÖØÓÃͬһ¾ä±ú¡£
Äú¿ÉÒÔʹÓÃÈ«¾Ö±äÁ¿´æ´¢Êý¾Ý¿â¾ä±ú£¬µ«ÊÇ£¬¸Ã·½·¨½öÊÊÓÃÓÚ½ÏСµÄÓ¦ÓóÌÐò¡£ÔڽϴóµÄÓ¦ÓóÌÐòÖУ¬Ó¦±ÜÃâʹÓÃÈ«¾Ö±äÁ¿£¬²¢Ê¹ÓöÔÏóºÍ·½·¨·ÃÎÊ×ÊÔ´¡£
¹Û²ìÕßģʽΪÄúÌṩÁ˱ÜÃâ×é¼þÖ®¼ä½ôÃÜñîºÏµÄÁíÒ»ÖÖ·½·¨¡£¸Ãģʽ·Ç³£¼òµ¥£ºÒ»¸ö¶ÔÏóͨ¹ýÌí¼ÓÒ»¸ö·½·¨£¨¸Ã·½·¨ÔÊÐíÁíÒ»¸ö¶ÔÏ󣬼´¹Û²ìÕß ×¢²á×Ô¼º£©Ê¹±¾Éí±äµÃ¿É¹Û²ì¡£µ±¿É¹Û²ìµÄ¶ÔÏó¸ü¸Äʱ£¬Ëü»á½«ÏûÏ¢·¢Ë͵½ÒÑ×¢²áµÄ¹Û²ìÕß¡£ÕâЩ¹Û²ìÕßʹÓøÃÐÅÏ¢Ö´ÐеIJÙ×÷Óë¿É¹Û²ìµÄ¶ÔÏóÎ޹ء£½á¹ûÊǶÔÏó¿ÉÒÔÏ໥¶Ô»°£¬¶ø²»±ØÁ˽âÔÒò¡£
Ò»¸ö¼òµ¥Ê¾ÀýÊÇϵͳÖеÄÓû§ÁÐ±í¡£
Çåµ¥ 4 ÖеĴúÂëÏÔʾһ¸öÓû§ÁÐ±í£¬Ìí¼ÓÓû§Ê±£¬Ëü½«·¢ËͳöÒ»ÌõÏûÏ¢¡£Ìí¼ÓÓû§Ê±£¬Í¨¹ý·¢ËÍÏûÏ¢µÄÈÕÖ¾¹Û²ìÕß¿ÉÒÔ¹Û²ì´ËÁÐ±í¡£
Çåµ¥ 4. Observer.php
<?php interface IObserver { function onChanged( $sender, $args ); } interface IObservable { function addObserver( $observer ); } class UserList implements IObservable { private $_observers = array(); public function addCustomer( $name ) { foreach( $this->_observers as $obs ) $obs->onChanged( $this, $name ); } public function addObserver( $observer ) { $this->_observers []= $observer; } } class UserListLogger implements IObserver { public function onChanged( $sender, $args ) { echo( "'$args' added to user list\n" ); } } $ul = new UserList(); $ul->addObserver( new UserListLogger() ); $ul->addCustomer( "Jack" ); ?>
´Ë´úÂ붨ÒåËĸöÔªËØ£ºÁ½¸ö½Ó¿ÚºÍÁ½¸öÀà¡£IObservable
½Ó¿Ú¶¨Òå¿ÉÒÔ±»¹Û²ìµÄ¶ÔÏó£¬UserList
ʵÏָýӿڣ¬ÒԱ㽫±¾Éí×¢²áΪ¿É¹Û²ì¡£IObserver
ÁÐ±í¶¨ÒåҪͨ¹ýÔõÑùµÄ·½·¨²ÅÄܳÉΪ¹Û²ìÕߣ¬UserListLogger
ʵÏÖ IObserver
½Ó¿Ú¡£Í¼ 4 µÄ UML ÖÐչʾÁËÕâЩԪËØ¡£
ͼ 4. ¿É¹Û²ìµÄÓû§ÁбíºÍÓû§ÁбíʼþÈÕÖ¾³ÌÐò
Èç¹ûÔÚÃüÁîÐÐÖÐÔËÐÐËü£¬Äú½«¿´µ½ÒÔÏÂÊä³ö£º
% php observer.php 'Jack' added to user list % |
²âÊÔ´úÂë´´½¨ UserList
£¬²¢½« UserListLogger
¹Û²ìÕßÌí¼Óµ½ÆäÖС£È»ºóÌí¼ÓÒ»¸öÏû·ÑÕߣ¬²¢½«ÕâÒ»¸ü¸Ä֪ͨ UserListLogger
¡£
ÈÏʶµ½ UserList
²»ÖªµÀÈÕÖ¾³ÌÐò½«Ö´ÐÐʲô²Ù×÷ºÜ¹Ø¼ü¡£¿ÉÄÜ´æÔÚÒ»¸ö»ò¶à¸öÖ´ÐÐÆäËû²Ù×÷µÄÕìÌý³ÌÐò¡£ÀýÈ磬Äú¿ÉÄÜÓÐÒ»¸öÏòÐÂÓû§·¢ËÍÏûÏ¢µÄ¹Û²ìÕߣ¬»¶ÓÐÂÓû§Ê¹ÓøÃϵͳ¡£ÕâÖÖ·½·¨µÄ¼ÛÖµÔÚÓÚ UserList
ºöÂÔËùÓÐÒÀÀµËüµÄ¶ÔÏó£¬ËüÖ÷Òª¹Ø×¢ÔÚÁбí¸ü¸Äʱά»¤Óû§ÁÐ±í²¢·¢ËÍÏûÏ¢ÕâÒ»¹¤×÷¡£
´Ëģʽ²»ÏÞÓÚÄÚ´æÖеĶÔÏó¡£ËüÊÇÔڽϴóµÄÓ¦ÓóÌÐòÖÐʹÓõÄÊý¾Ý¿âÇý¶¯µÄÏûÏ¢²éѯϵͳµÄ»ù´¡¡£
ÃüÁîÁ´ ģʽÒÔËÉÉ¢ñîºÏÖ÷ÌâΪ»ù´¡£¬·¢ËÍÏûÏ¢¡¢ÃüÁîºÍÇëÇ󣬻òͨ¹ýÒ»×é´¦Àí³ÌÐò·¢ËÍÈÎÒâÄÚÈÝ¡£Ã¿¸ö´¦Àí³ÌÐò¶¼»á×ÔÐÐÅжÏ×Ô¼ºÄÜ·ñ´¦ÀíÇëÇó¡£Èç¹û¿ÉÒÔ£¬¸ÃÇëÇó±»´¦Àí£¬½ø³ÌÍ£Ö¹¡£Äú¿ÉÒÔΪϵͳÌí¼Ó»òÒƳý´¦Àí³ÌÐò£¬¶ø²»Ó°ÏìÆäËû´¦Àí³ÌÐò¡£
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿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¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ