³Ð½ÓÉÏÎÄPHP ÔÓ̸¡¶Öع¹-¸ÄÉƼÈÓдúÂëµÄÉè¼Æ¡·Ö® ÖØÐÂ×éÖ¯ÄãµÄº¯Êý £¬¼ÌÐø˵Öع¹·½ÃæµÄÄÚÈÝ
˼άµ¼Í¼
Ë÷Òý£º
Ø Move Method£¨°áÒƺ¯Êý£©
Ø Move Field £¨°áÒÆÖµÓò£©
Ø Extract Class £¨ÌáÁ¶Àࣩ
Ø Inline Class £¨½«ÀàÄÚÁª»¯£¬¾ÍÊǰѵ±Ç°µÄÀàºÏ²¢µ½ÆäËûÀàÖУ©
Ø Hide Delegate £¨Òþ²ØίÍйØϵ£©
Ø Remove Middle Man £¨ ÒƳýÖмäÈË£©
Ø Introduce Foreign Method £¨ÒýÈëÍâ¼Óº¯Êý£©
Ø Introduce Local Extension £¨ÒýÈë±¾µØÀ©Õ¹£©
½éÉÜ
³Ð½ÓÉÏÎÄPHP ÔÓ̸¡¶Öع¹-¸ÄÉƼÈÓдúÂëµÄÉè¼Æ¡·Ö® ÖØÐÂ×éÖ¯ÄãµÄº¯Êý £¬¼ÌÐø˵Öع¹·½ÃæµÄÄÚÈÝ¡£
רҵÊõÓï
delegate£ºÎ¯ÍÐ
encapsulate:·â×°
introduce£ºÒýÈë
wrapper£º¸²¸Ç
Ç°ÑÔ
¡±¾ö¶¨°ÑÔðÈηÅÔÚÄÄÀï¡°¡ª¡ªÔËÓÃÖع¹¸Ä±äÔÏȵÄÉè¼Æ¡£
½âÊÍ£º
¡¡¡¡1¡¢Class³Ðµ£¹ý¶à¶øÓ·Öײ»¿°¡ª¡ªExtract Class½«Ò»²¿·ÖÔðÈηÖÀë³öÈ¥¡£
¡¡¡¡2¡¢ClassûÓге£×ã¹»¶àµÄÔðÈΣ¬²»ÔÙÓе¥¶À´æÔÚµÄÀíÓÉ¡ª¡ªInline Class½«ËüÈÚÈëÁíÒ»¸öClass¡£
¡¡¡¡3¡¢ClassʹÓÃÁíÒ»¸öClass¡ª¡ªHide DelegateÒþ²Ø¹Øϵ¡£
¡¡¡¡4¡¢³Ð½Ó£¨3£©£¬Èç¹ûClientͨ¹ýMiddle Man µ÷ÓúܶàµÄDelegate ClassµÄº¯Êý£¨ÕâÀïÖ»ÊǼòµ¥µ÷Óã¬Ö»×öÌøת£¬¶øMiddle ManûÓÐ×öÌ«¶àµÄÒµÎñÂß¼£¬Èç10¸öDelegate ClassÖеÄMethod¶ÔÓ¦10¸öMiddle ManµÄMethod£©¡ª¡ªRemove Middle Man£¬Ö±½ÓʹÓÃDelegate Class£¬¿ÉÒÔ²¿·ÖʹÓÃDelegate Method¡£
Move Method
Èç¹ûÒ»¸öÀàÖеķ½·¨ÓëÁíÒ»¸öÀàÓкܶàµÄ½»Á÷£¬ÄÇôÎÒÃǾÍÔÚÁíÒ»¸öÀàÖн¨Á¢Ò»¸öÓÐÀàËƹ¦ÄܵÄк¯Êý£¬½«¾Éº¯Êý±ä³ÉÒ»¸öµ¥´¿µÄDelegating Method£¬ »òÊǽ«¾Éº¯ÊýÒƳý¡£
Ààͼ:
¶¯»ú£º
¡¡¡¡1¡¢Èç¹ûÒ»¸öÀàÓëÁíÒ»¸öÀàÓи߶ÈñîºÏ£¬ÎҾͻáMove Method¡£¡ª¡ªclass¸ü¼òµ¥£¬¸ü¸É¾»ÀûÂäµÄʵÏÖϵͳ½»¸¶µÄÈÎÎñ¡£
¡¡¡¡2¡¢Òƶ¯Ò»Ð©ÖµÓò£¬¾ÍÒª¼ì²éÊÇ·ñʹÓÃÁíÒ»¸öÀàµÄ´ÎÊý±ØʹÓÃËùפ¶ÔÏóµÄ´ÎÊý»¹¶à¡£
Move Field
×´¿ö£ºÄãµÄclassÖеÄfield±»ÁíÒ»¸öclass¸ü¶àµÄÓõ½¡£ÄÇôÔÚÁíÒ»¸öclassÀィÁ¢new field£¬Ð޸ľɵÄfield¡£
Extract Class
×´¿ö£ºÒ»¸öÀà×öÁËÁ½¸öÀà×öµÄÊ£¬ÄÇô½¨Á¢Ò»¸öÐÂClass£¬½«Ïà¹ØµÄFieldºÍMethod´Ó¾ÉClassÒƵ½ÐÂClass¡£
Inline Class
×´¿ö£ºÄãµÄij¸öClassûÓÐ×öÌ«¶àÊÂÇ飨ûÓге£×ã¹»ÔðÈΣ©£¬ÄÇô½«ClassµÄËùÓÐÌØÐÔ°áÒƵ½ÁíÒ»¸öClassÖУ¬È»ºóÒƳýÔClass¡£
¶¯»ú£ºInline ClassÓëExtract ClassÏà·´¡£¡ª¡ª°ÑExtract ClassÀý×Ó·´¹ýÈ¥£¬ÒòΪPhoneNumberÖ»ÓÃ×÷¶ÁÈ¡codeºÍnumber¡£
Hide Delegate
×´¿ö£º¿Í»§Ö±½Óµ÷ÓÃServer ObjectµÄDelegate ClassµÄMethod£¬ÄÇôÔÚServer¶Ë½¨Á¢¿Í»§ËùÐèµÄº¯ÊýMethod£¬ÓÃÒÔÒþ²ØίÍйØϵ¡£
ѧ¹ý¶ÔÏó¼¼ÊõµÄÈ˶¼ÖªµÀ£¬ËäÈ»phpÔÊÐíÄ㽫fieldÉùÃ÷Ϊpublic£¬µ«Ä㻹Ӧ¸ÃÒþ²Øfield£¨private£©¡£ËæמÑéÈÕ½¥·á¸»£¬Óиü¶àÖµµÃ·â×°µÄ¶«Î÷¡£
¿´ÏÂÃæÒ»¸öÀý×Ó£º
$person->getDepartment()->getManager()Ã÷ÏÔ½Ò¶ÁË£¬ÒªÏëÕÒµ½XiaocaiµÄÁìµ¼£¬±ØÐëÒª¾¹ýdepartment£¬ËùÒÔÎÒÃÇÒª×öµÄÊÂÒþ²Ødepartment¡£¡ª¡ª¿ÉÒÔ¼õÉÙñîºÏÐÔ¡£
Remove Middle Man
×´¿ö£ºÈç¹ûij¸öClass×öÁ˹ý¶àµÄSimple Delegate£¬ÄÇôÎÒÃǾÍÖ±½Óµ÷ÓÃDelegate Class¡£
¶¯»ú£ºÔÚHide DelegateÖеÄÀý×ÓÀïµ±DepartmentÓиü¶àз½·¨µÄʱºò£¬ÎÒÃÇΪÁËHide Delegate£¬¾ÍÒª±ØÐëÔÚPersonÀïÌí¼ÓÏàÓ¦µÄ·½·¨×öDelegateÖ®Óá£ÕâʱºòµÄPersonÍêÈ«±ä³ÉÁËÒ»¸öMiddle Man£¬´ËʱÎÒÃǾÍÓ¦¸ÃÖ±½Óµ÷ÓÃDelegate Class¡ª¡ªDepartment¡£
Öع¹µÄÒâÒå¾ÍÔÚÓÚ£ºÄãÓÀÔ¶²»±Ø˵¶Ô²»Æð£¬Ö»ÒªÄã°Ñ³öÎÊÌâµÄµØ·½ÐÞ²¹ºÃ¾ÍÐÐÁË¡£
Introduce Foreign Method
×´¿ö£ºÓÐÒ»¸öÀàClientÐèҪʹÓõÄÀàPreviousEndÖÐÒ»¸ö¶îÍ⺯Êý£¬µ«ÄãÎÞ·¨ÐÞ¸ÄÕâ¸öÀàPreviousEnd£¬ÄÇôÄã¾ÍÔÚClientÖн¨Á¢Ò»¸öº¯Êý£¬²¢ÒÔÒ»¸öPreviousEndʵÌå×öΪ²ÎÊý¡£
Introduce Local Extention
×´¿ö£ºÄãµÄClassÐèҪһЩ¶îÍ⺯Êý£¬µ«Äã²»ÄÜÐ޸ĵ±Ç°µÄÀ࣬ÄÇô½¨Á¢Ò»¸öÐÂClass£¬Ê¹Ëü°üº¬ÕâЩº¯Êý¡£Ê¹ÓÃSubclass »ò Wrapper¡£¡ª¡ªÕâ¸öÒ»°ãÓÃÓÚÄãÎÞ·¨ÐÞ¸ÄÔ´ÂëµÄÇé¿öÏÂʹÓá£
ÄÃÉÏÃæIntroduce Foreign MethodÀý×ÓÀ´Ëµ
ʹÓÃSubclass·½·¨
»òʹÓÃWrapper
×ܽá
ÐèҪעÒâһϣ¬¡°Extract Class¡±ºÍ¡°Inline Class¡±£¬¡±Hide Delegate¡°ºÍ¡±Remove Middle Man"£¬¶¼ÊÇÏà·´µÄ¹ý³Ì£¬¾ßÌåÀí½â£¬¿ÉÒÔ¿´Ç°ÑÔÖеÄÄÇÕÅÁ÷³Ìͼ¡£
¡°Hide Delegate"ÎÒÃdz£ÓÃÓÚʹÓÃÉÙÁ¿µÄ¡±Delegate Method¡°µÄʱºò£¬¶ø¡±Remove Middle Man¡°£¬ÓÃÓÚµ÷ÓúܶࡱDelegate Method¡°µÄʱºò£¬ÎÒÃÇ¿ÉÒÔÖ±½ÓʹÓÃDelegate Class£¬½øÐе÷Ó㬶øÓеÄDelegate MethodÎÒÃÇÊÓÇé¿ö±£ÁôÒ»²¿·Ö¡£
¡°Extract Class¡±ºÍ¡°Inline Class¡±£¬"Extract Class"¾³£ÓÃÓڳе£Äǹý¶àÔðÈζø±äµÃÓ·Öײ»¿°µÄClassÖУ¬¶ø¡°Inline Class¡±¾³£ÓÃÓÚµ±Ç°µÄÕâ¸öÀࡱ̫²»¸ºÔðµÄ¡°Ê±ºòʹÓᣡª¡ªÎÒ¸öÈËÊÇÄþÔ¸¡°Extract Class¡±£¬Ò²²»Ô¸¡°Inline Class¡±¡£ |