IBM Korea Skip to main content
       IBM Ȩ    |  Á¦Ç° & ¼­ºñ½º  |  °í°´Áö¿ø & ´Ù¿î·Îµå  |  È¸¿ø°¡ÀÔ  

IBM developerWorks > ÀÚ¹Ù
developerWorks

¸®ÆÑÅ丵(Refactoring)
EclipseÀÇ ÀÚµ¿È­ ¸®ÆÑÅ丵 ±â´É

Level: Intermediate

David Gallardo
¼ÒÇÁÆ®¿þ¾î ÄÁ¼³ÅÏÆ®
2003³â 9¿ù 9ÀÏ

Eclipse´Â °­·ÂÇÑ ÀÚµ¿ ¸®ÆÑÅ丵À» Á¦°øÇÑ´Ù. ÀÌ°ÍÀ» ÅëÇØ ÀÚ¹Ù ¿¤¸®¸ÕÆ®¸¦ Àç¸í¸íÇÏ°í, Ŭ·¡½º¿Í ÆÐÅ°Áö¸¦ ¿Å±â¸ç ½ÇÁ¦ Ŭ·¡½º¿¡¼­ ÀÎÅÍÆäÀ̽º¸¦ ¸¸µé°í Áßø Ŭ·¡½º¸¦ »óÀ§·¹º§ Ŭ·¡½º·Î º¯È¯ÇÏ°í ±¸ ¸Þ¼ÒµåÀÇ ÄÚµå ¼½¼Ç¿¡¼­ »õ·Î¿î ¸Þ¼Òµå¸¦ °¡Á®¿Ã ¼ö ÀÖ´Ù. EclipseÀÇ ¸®ÆÑÅ丵 Åø¿¡ Àͼ÷ÇØÁø´Ù¸é »ý»ê¼ºÀ» Çâ»óÇÒ ¼ö ÀÖ´Â ÁÁÀº ¹æ¹ýÀÌ´Ù.

¸®ÆÑÅ丵
¸®ÆÑÅ丵(Refactoring)Àº ÇÁ·Î±×·¥ÀÇ ±â´ÉÀº º¯È­½ÃÅ°Áö ¾Ê°í ÇÁ·Î±×·¥ÀÇ ±¸Á¶¸¦ º¯È­½ÃÅ°°í ÀÖ´Ù. ¸®ÆÑÅ丵Àº °­·ÂÇÑ ±â¼úÀÌÁö¸¸ Á¶½É½º·´°Ô ´Ù·ïÁ®¾ß ÇÑ´Ù. °¡Àå À§ÇèÇÑ °ÍÀº ¿¡·¯µéÀÌ °©Àڱ⠹߻ýÇÒ ¼ö ÀÖ´Ù. ƯÈ÷ ¸®ÆÑÅ丵À» ¼öµ¿À¸·Î ¼öÇàÇÒ ¶§ ±×·¸´Ù. ¹Ù·Î ÀÌ°ÍÀÌ ¸®ÆÑÅ丵ÀÇ ÁÖ¿ä ºñÆÇ ¿ä¼ÒÀÌ´Ù.

Äڵ带 ¸®ÆÑÅ丵ÇÏ´Â ¿©·¯°¡Áö ÀÌÀ¯°¡ ÀÖ´Ù. ù ¹ø°´Â Àü¼³ ¶§¹®ÀÌ´Ù. ÈǸ¢ÇÑ Á¦Ç°À» À§ÇÑ ÁøºÎÇÑ ¿À·¡µÈ ÄÚµå ±â¹ÝÀº Àü½ÂµÇ°Å³ª ±×·¸Áö ¾Ê´Ù¸é ½Åºñ½º·´°Ô ³ªÅ¸³­´Ù. ±×¸®°í ¿ø·¡ °³¹ßÆÀÀº »ç¶óÁ³´Ù. »õ·Î¿î ±â´ÉµéÀ» °®Ãá »õ·Î¿î ¹öÀüÀÌ ¸¸µé¾îÁ®¾ß ÇÏÁö¸¸ ±× ÄÚµå´Â ´õ ÀÌ»ó ÀÌÇØÇÒ ¼ö ¾ø´Â °ÍÀÌ µÇ°í ¸¸´Ù. »õ·Î¿î °³¹ßÆÀÀº ¹ã³·À¸·Î ÀÏÇϸç ÀÌ°ÍÀ» Æǵ¶ÇÏ°í ¸ÅÇÎÇÏ¸ç ¸¹Àº °èȹ°ú µðÀÚÀÎ ÈÄ¿¡ Äڵ带 »ê»êÁ¶°¢À¸·Î ¸¸µç´Ù. ¸¶Ä§³» ±×µéÀº »õ·Î¿î ¹öÀü¿¡ µû¶ó ÀÌ ¸ðµÎ¸¦ ´Ù½Ã Á¦ÀÚ¸®¿¡ °®´Ù ³õ´Â´Ù. ÀÌ°ÍÀº ¿µ¿õÀû ½ºÄÉÀÏÀÇ ¸®ÆÑÅ丵ÀÌ°í ÀϺθ¸ÀÌ »ì¾Æ³²¾Æ ÀÌ À̾߱⸦ ÇÒ ¼ö ÀÖ´Ù.

º¸´Ù Çö½ÇÀûÀÎ ½Ã³ª¸®¿À´Â µðÀÚÀÎ º¯È­¸¦ ÇÊ¿ä·ÎÇÏ´Â ÇÁ·ÎÁ§Æ®¿¡ »õ·Î¿î ¿ä±¸»çÇ×ÀÌ ¸¸µé¾îÁ³´Ù´Â °ÍÀÌ´Ù. ÀÌ·¯ÇÑ ¿ä±¸»çÇ×ÀÌ ¿ø·¡ °èȹÀÇ ºÎÁÖÀÇÇÑ °£°ú ¶§¹®ÀÎÁö ¶Ç´Â ¹Ýº¹ÀûÀÎ Á¢±Ù¹æ½ÄÀÌ °³¹ß ÇÁ·Î¼¼½º Àü¹Ý¿¡ °ÉÃÄ ¿ä±¸»çÇ×µéÀ» Á¤±³ÇÏ°Ô ¸¸µé¾î³»´Âµ¥ »ç¿ëµÇ¾ú±â ¶§¹®ÀÌÁö´Â ºÒºÐ¸íÇÏ´Ù. ÀÌ°ÍÀº ÈξÀ ÀÛÀº ½ºÄÉÀÏÀÇ ¸®ÆÑÅ丵ÀÌ´Ù. ±×¸®°í ¿©±â¿¡´Â ÀϹÝÀûÀ¸·Î Ŭ·¡½º °èÃøÀ» º¯°æÇϴ°ÍÀÌ Æ÷ÇԵǾîÀִµ¥ ¾Æ¸¶µµ ÀÎÅÍÆäÀ̽º ¶Ç´Â Ãß»ó Ŭ·¡½º µµÀÔ, Ŭ·¡½º ³ª´©±â, Ŭ·¡½º ÀçÁ¶Á¤µîÀ» ÅëÇØ ¼öÇàµÈ´Ù.

¸¶Áö¸·À¸·Î, ÀÚµ¿È­µÈ ¸®ÆÑÅ丵 ÅøÀÌ »ç¿ë°¡´ÉÇÏ´Ù¸é, ù ¹ø° ´Ü°è¿¡¼­ ÄÚµå »ý¼ºÀ» À§ÇÑ Áö¸§±æ·Î¼­ÀÇ ¸®ÆÑÅ丵À» ÇÏ´Â ÀÌÀ¯°¡ µÇ°Ú´Ù. ¸®ÆÑÅ丵À» ÀÌ·¸°Ô »ç¿ëÇÑ´Ù¸é Åø¿¡ Àͼ÷ÇÏ´Ù¸é È¿°úÀûÀ¸·Î ½Ã°£À» Àý¾àÇÒ ¼ö ÀÖ´Ù.

Äڵ尡 ±úÁö´Â À§ÇèÀ» °¨¼Ò½Ãų ¼ö ÀÖ´Â µÎ °¡Áö Áß¿äÇÑ ¹æ¹ýÀÌ ÀÖ´Ù. Çϳª´Â ÄÚµå¿ë ´ÜÀ§ Å×½ºÆ® ¼¼Æ®¸¦ °®Ãß´Â °ÍÀÌ´Ù. ÀÌ ÄÚµå´Â ¸®ÆÑÅ丵 ÀüÈÄ¿¡ °ÉÃÄ Å×½ºÆ®¸¦ Åë°úÇؾßÇÑ´Ù. µÎ ¹ø° ¹æ¹ýÀº EclipseÀÇ ¸®ÆÑÅ丵 ±â´É °°Àº ÀÚµ¿È­µÈ ÅøÀ» »ç¿ëÇÏ¿© ¸®ÆÑÅ丵À» ¼öÇàÇÏ´Â °ÍÀÌ´Ù.

Àüü Å×½ºÆðú ÀÚµ¿È­µÈ ¸®ÆÑÅ丵ÀÇ °áÇÕÀº Ưº°È÷ °­·ÂÇÏ¸ç ¾ðÁ¦³ª À¯¿ëÇÑ Åø·Î º¯¸ðµÈ´Ù. ±â´ÉÀ» Ãß°¡Çϰųª °ü¸®´É·ÂÀ» Çâ»ó½ÃÅ°±â À§ÇØ ±â´ÉÀ» º¯È­½ÃÅ°Áö ¾Ê°í ÄÚµåÀÇ ±¸Á¶¸¦ º¯°æÇÏ´Â ´É·ÂÀº Äڵ带 ¼³°èÇÏ°í °³¹ßÇÏ´Â ¹æ½Ä¿¡ Å« ¿µÇâÀ» ¹ÌÄ¥ ¼ö ÀÖ´Ù.

EclipseÀÇ ¸®ÆÑÅ丵 À¯Çü
EclipseÀÇ ¸®ÆÑÅ丵 ÅøÀº Å©°Ô ¼¼ °¡Áö ¹üÁÖ·Î ³ª´­ ¼ö ÀÖ´Ù.(ÀÌ°ÍÀº Refactoring ¸Þ´º¿¡ ³ªÅ¸³ª´Â ¼ø¼­ÀÌ´Ù):

  1. ÄÚµåÀÇ À̸§°ú ¹°¸®Àû Á¶Á÷À» º¯°æÇÏ°í, Àç¸í¸í Çʵå, º¯¼ö, Ŭ·¡½º, ÀÎÅÍÆäÀ̽º¸¦ Ãß°¡ÇÏ°í, ÆÐÅ°Áö¿Í Ŭ·¡½º ¿Å±â±â
  2. Ŭ·¡½º ·¹º§¿¡¼­ ÄÚµåÀÇ ³í¸®Àû Á¶Á÷À» º¯°æÇÏ°í, À͸íÀÇ Å¬·¡½ºµéÀ» Áßø Ŭ·¡½º·Î ¿Å±â¸ç, Áßø Ŭ·¡½ºµéÀ» »óÀ§ ·¹º§ Ŭ·¡½º·Î ÀüȯÇÏ°í ±¸Ã¼ Ŭ·¡½º¿¡¼­ ÀÎÅÍÆäÀ̽º¸¦ ¸¸µé°í Ŭ·¡½º¿¡¼­ ÇÏÀ§ Ŭ·¡½º ¶Ç´Â »óÀ§ Ŭ·¡½º·Î ¸Þ¼Òµå ¶Ç´Â ÇÊµå ¿Å±â±â
  3. Ŭ·¡½º ³»¿¡ ÀÖ´Â ÄÚµå º¯°æÇÏ°í, ·ÎÄà º¯¼ö¸¦ Ŭ·¡½º Çʵå·Î º¯È¯ÇÏ°í ¸Þ¼Òµå³»ÀÇ ¼±ÅÃµÈ Äڵ带 °³º° ¸Þ¼Òµå·Î ÀüȯÇÏ°í getter¿Í setter ¸Þ¼Òµå »ý¼ºÇϱâ

¸¹Àº ¸®ÆÑÅ丵ÀÌ ÀÌ·¯ÇÑ ¼¼ °¡Áö ¹üÁÖ¿¡ µü ¸Â¾Æ¶³¾îÁö´Â °ÍÀº ¾Æ´Ï´Ù. ƯÈ÷ ¼¼ ¹ø° Ä«Å×°í¸®¿¡ ÀÖ´Â Change Method Signature°¡ ±×·¸´Ù. ÀÌ ¿¹¿Ü¸¦ Á¦¿ÜÇÏ°í ³ª¸ÓÁö ¼½¼ÇµéÀº ÀÌ ¼ø¼­´ë·Î EclipseÀÇ ¸®ÆÑÅ丵 ÅøÀ» ¼³¸íÇÒ °ÍÀÌ´Ù.

¹°¸®Àû À籸¼º(reorganization)°ú Àç¸í¸í(renaming)
Ưº°ÇÑ Åø ¾øÀÌ ÆÄÀÏ ½Ã½ºÅÛ¿¡ ÀÖ´Â ÆÄÀϵéÀ» Àç¸í¸íÇϰųª ¿Å±æ ¼ö ÀÖ´Ù. ÇÏÁö¸¸ ÀÚ¹Ù ¼Ò½º ÆÄÀÏ·Î ÀÌÀÏÀ» ¼öÇàÇÏ·Á¸é import ¶Ç´Â package ¹®À» ¾÷µ¥ÀÌÆ® Çϱâ À§ÇØ ¸¹Àº ÆÄÀϵéÀ» ÆíÁýÇØ¾ß ÇÑ´Ù. ÅؽºÆ® ¿¡µðÅÍÀÇ °Ë»öÀ» »ç¿ëÇÏ¿© Ŭ·¡½º, ¸Þ¼Òµå, º¯¼ö¸¦ ½±°Ô Àç¸í¸í ÇÒ ¼ö ÀÖ°í ±â´ÉÀ» ´ëü½Ãų ¼ö ÀÖ´Ù. ÇÏÁö¸¸ ´Ù¾çÇÑ Å¬·¡½ºµéÀÌ °°Àº À̸§À» °¡Áø ¸Þ¼Òµå ¶Ç´Â º¯¼ö¸¦ °®±â ¶§¹®¿¡ Á¶½ÉÇØ¾ß ÇÑ´Ù. ¸ðµç ÀνºÅϽº°¡ Á¤È®È÷ ±¸ºÐµÇ°í º¯°æµÇµµ·Ï È®ÀÎÇØ¾ß ÇÑ´Ù.

EclipseÀÇ Rename°ú Move´Â »ç¿ëÀÚ°¡ °³ÀÔÇÏÁö ¾Ê°í ÀÌ·¯ÇÑ º¯°æµéÀ» Áö´ÉÀûÀ¸·Î ¼öÇàÇÒ ¼ö ÀÖ´Ù. Eclipse´Â ÄÚµå ¹®¹ýÀ» ÀÌÇØÇÏ°í ƯÁ¤ ¸Þ¼Òµå, º¯¼ö, Ŭ·¡½º À̸§¿¡ ´ëÇÑ ·¹ÆÛ·±½º¸¦ ±¸ºÐÇÒ ¼ö Àֱ⠶§¹®ÀÌ´Ù.

Äڵ尡 ¿ø·¡ °èȹ°ú´Â ´Ù¸£°Ô ÀÛµ¿Çϵµ·Ï º¯°æµÇ¾ú±â ¶§¹®¿¡ ºÎÀûÀýÇÑ À̸§À̳ª ¿ÀµµµÈ À̸§À» °¡Áø Äڵ带 ã´Â °ÍÀº ¸Å¿ì ±âº»ÀûÀÎ ÀÏÀÌ´Ù. ¿¹¸¦ µé¾î ÆÄÀÏ¿¡¼­ ƯÁ¤ ´Ü¾î¸¦ ã´Â ÇÁ·Î±×·¥Àº InputStreamÀ» ¾ò±â À§ÇØ URL Ŭ·¡½º¸¦ »ç¿ëÇÔÀ¸·Î¼­ À¥ ÆäÀÌÁö¿Í ÇÔ²² ÀÛµ¿Çϵµ·Ï È®ÀåµÉ ¼ö ÀÖ´Ù. ÀÌ ÀÎDz ½ºÆ®¸²ÀÌ ¿ø·¡ ÆÄÀϷΠȣÃâµÇ¾ú´Ù¸é Á»´õ ÀϹÝÀûÀÎ ¼ºÁúÀ» ¹Ý¿µÇϵµ·Ï º¯°æµÇ¾î¾ß ÇÑ´Ù. °³¹ßÀÚµéÀº ÀÌ °°Àº º¯°æ ÀÛ¾÷À» ½ÇÆÐÇÏ°ï ÇÑ´Ù.

ÀÚ¹Ù ¿¤¸®¸ÕÆ®¸¦ Àç¸í¸íÇÏ·Á¸é Package Explorer ºä¿¡ ÀÖ´Â renameÀ» Ŭ¸¯Çϰųª ÀÚ¹Ù ¼Ò½º ÆÄÀÏ¿¡¼­ À̸¦ ¼±ÅÃÇÑ ´ÙÀ½ Refactor > RenameÀ» ¼±ÅÃÇÑ´Ù. ´ÙÀ̾ó·Î±× ¹Ú½º¿¡¼­ »õ·Î¿î À̸§À» ¼±ÅÃÇÏ°í Eclipse°¡ ÀÌ À̸§¿¡ ´ëÇÑ ·¹ÆÛ·±½ºµµ º¯°æÇÏ´ÂÁöÀÇ ¿©ºÎ¸¦ ¼±ÅÃÇÑ´Ù. µð½ºÇ÷¹À̵Ǵ Á¤È®ÇÑ Çʵå´Â ¼±ÅÃÇÑ ¿¤¸®¸ÕÆ® À¯Çü¿¡ ÀÇÁ¸ÇÑ´Ù. ¿¹¸¦ µé¾î getter¿Í setter ¸Þ¼Òµå¸¦ °¡Áø Çʵ带 ¼±ÅÃÇϸé ÀÌ »õ·Î¿î Çʵ带 ¹Ý¿ªÇϱâ À§ÇØ ÀÌ ¸Þ¼ÒµåµéÀÇ À̸§À» ¾÷µ¥ÀÌÆ® ÇÒ ¼ö ÀÖ´Ù. (±×¸² 1).

±×¸² 1. ·ÎÄà º¯¼öÀÇ Àç¸í¸í
Renaming a local variable

¸ðµç Eclipse ¸®ÆÑÅ丵°ú ¸¶Âù°¡Áö·Î ¸®ÆÑÅ丵À» ¼öÇàÇϴµ¥ ÇÊ¿äÇÑ ¸ðµç °ÍÀ» ¼³Á¤ÇÑ ÈÄ¿¡ Preview¸¦ ´­·¯ º¯°æ»çÇ×À» º¼ ¼ö ÀÖ´Ù. ºñ±³ ´ÙÀ̾ó·Î±×·Î µÇ¾îÀִµ¥ °ÅºÎ ¶Ç´Â ¸ðµç °³º° ÆÄÀÏÀÇ º¯°æ»çÇ× ¸¶´Ù °ÅºÎ ¶Ç´Â ½ÂÀÎÀ» ÇÒ ¼ö ÀÖ´Ù. Eclipse°¡ º¯°æÀÛ¾÷À» Á¤È®ÇÏ°Ô ¼öÇàÇß´Ù´Â È®½ÅÀÌ ÀÖ´Ù¸é OK¸¸ ´­·¯µµ µÈ´Ù. ºÐ¸íÈ÷ ¸®ÆÑÅ丵ÀÌ ¹«¾ùÀ» ¼öÇàÇÏ´ÂÁö È®½ÇÇÏÁö ¾Ê´Ù¸é ÇÁ¸®ºä¸¦ ¿ì¼± ´­·¯¾ß ÇÑ´Ù. ÇÏÁö¸¸ ÀÌ°ÍÀº Rename°ú Move °°Àº °£´ÜÇÑ ¸®ÆÑÅ丵¿¡´Â ÇØ´çÇÏÁö ¾Ê´Â´Ù.

Move´Â Rename°ú ºñ½ÁÇÏ´Ù: ÀÚ¹Ù ¿¤¸®¸ÕÆ®(´ë°Ô, Ŭ·¡½º)¸¦ ¼±ÅÃÇÏ°í ÀÌ°ÍÀÇ »õ·Î¿î À§Ä¡¸¦ ÁöÁ¤ÇÏ°í ·¹ÆÛ·±½ºÀÇ ¾÷µ¥ÀÌÆ® ¿©ºÎ¸¦ ÁöÁ¤ÇÑ´Ù. ±×·±´ÙÀ½ Preview¸¦ ¼±ÅÃÇØ º¯°æ »çÇ×À» °ËÅäÇϰųª OK¸¦ ´­·¯ ¸®ÆÑÅ丵À» Áï½Ã ¼öÇàÇÑ´Ù. (±×¸² 2).

±×¸² 2. ÇϳªÀÇ ÆÐÅ°Áö¿¡¼­ ´Ù¸¥ ÆÐÅ°Áö·Î Ŭ·¡½º ¿Å±â±â
Moving a class

¸î¸î Ç÷§Æû »ó¿¡¼­´Â (ÁÖ·Î Windows), ÇϳªÀÇ ÆÐÅ°Áö ¶Ç´Â Æú´õ¿¡¼­ ¶Ç ´Ù¸¥ ÆÐÅ°Áö³ª Æú´õ·Î Ŭ·¡½º¸¦ À̵¿ÇÒ ¼ö ÀÖ´Ù. Package Explorer ºä¿¡¼­ À̸¦ µå·¡±× & µå¶ø ¹æ½ÄÀ» ÀÌ¿ëÇÑ´Ù. ¸ðµç ·¹ÆÛ·±½ºµéÀº ÀÚµ¿À¸·Î ¾÷µ¥ÀÌÆ® µÉ °ÍÀÌ´Ù.

Ŭ·¡½º °ü°è ÀçÁ¤ÀÇ
EclipseÀÇ ¸®ÆÑÅ丵 ¼¼Æ®¸¦ ÀÌ¿ëÇϸé Ŭ·¡½º °ü°è¸¦ ÀÚµ¿À¸·Î º¯°æÇÒ ¼ö ÀÖ´Ù. ÀÌ·¯ÇÑ ¸®ÆÑÅ丵Àº Eclipse°¡ Á¦°øÇÏ´Â ´Ù¸¥ À¯ÇüÀÇ ¸®ÆÑÅ丵 ¸¸Å­ ÀϹÝÀûÀÎ °ÍÀº ¾Æ´ÏÁö¸¸ ¸Å¿ì º¹ÀâÇÑ Å½ºÅ©¸¦ ¼öÇàÇÑ´Ù´Â Á¡¿¡¼­ °¡Ä¡°¡ ÀÖ´Ù.

À͸íÀÇ Å¬·¡½º¿Í Áßø Ŭ·¡½º »ç¿ëÇϱâ
µÎ °³ÀÇ ¸®ÆÑÅ丵(Convert Anonymous Class to Nested & Convert Nested Type to Top Level)Àº ÇöÀç ¹üÀ§¿¡¼­ enclosing ¹üÀ§±îÁö Ŭ·¡½º¸¦ ¿Å±ä´Ù´Â Á¡¿¡¼­ ºñ½ÁÇÏ´Ù.

À͸íÀÇ Å¬·¡½º´Â Ŭ·¡½º À̸§À» ÁÖÁö¾Ê°í ÇÊ¿äÇÑ °÷¿¡ Ãß»ó Ŭ·¡½º³ª ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇϴ Ŭ·¡½º¸¦ ÀνºÅϽºÈ­ÇÒ ¼ö ÀÖ´Â Áö¸§±æÀÌ´Ù. »ç¿ëÀÚ ÀÎÅÍÆäÀ̽º¿¡ ¸®½º³Ê¸¦ ¸¸µé ¶§ ÀϹÝÀûÀ¸·Î »ç¿ëµÈ´Ù. BagÀÌ µÎ °³ÀÇ ¸Þ¼Òµå(get() & set())¸¦ ¼±¾ðÇÏ´Â °÷¿¡¼­ Á¤ÀÇµÈ ÀÎÅÍÆäÀ̽º¶ó°í °¡Á¤Çغ¸ÀÚ. (Listing 1).

Listing 1. Bag Ŭ·¡½º

public class BagExample
{
   void processMessage(String msg)
   {
      Bag bag = new Bag()
      {
         Object o;
         public Object get()
         {
            return o;
         }
         public void set(Object o)
         {
            this.o = o;
         }
      };
      bag.set(msg);
      MessagePipe pipe = new MessagePipe();
      pipe.send(bag);
   }
}

À͸íÀÇ Å¬·¡½º°¡ ³Ê¹« Ä¿Á®¼­ ÄÚµå Æǵ¶ÀÌ ºÒ°¡´ÉÇÏ°Ô µÇ¸é À͸íÀÇ Å¬·¡½º¸¦ Á¤½Ä Ŭ·¡½º·Î ¸¸µå´Â °ÍÀ» °í·ÁÇغÁ¾ß ÇÑ´Ù. ĸ½¶À» À¯ÁöÇÏ·Á¸é »óÀ§ ·¹º§ Ŭ·¡½º º¸´Ù´Â Áßø Ŭ·¡½º·Î ¸¸µé¾î¾ß ÇÑ´Ù. À͸í Ŭ·¡½º ¾È¿¡¼­ Ŭ¸¯ÇÏ¿© Refactor > Convert Anonymous Class to Nested¸¦ ¼±ÅÃÇÑ´Ù. Ŭ·¡½º À̸§À» ÀÔ·ÂÇÏ°í Preview ¶Ç´Â OK¸¦ ¼±ÅÃÇÑ´Ù. (Listing 2).

Listing 2. ¸®ÆÑÅ丵ÀÌ ¼öÇàµÈ Bag Ŭ·¡½º

public class BagExample
{
   private final class BagImpl implements Bag
   {
      Object o;
      public Object get()
      {
         return o;
      }
      public void set(Object o)
      {
         this.o = o;
      }
   }
       
   void processMessage(String msg)
   {
     Bag bag = new BagImpl();
     bag.set(msg);
     MessagePipe pipe = new MessagePipe();
     pipe.send(bag);
   }
}

Convert Nested Type to Top LevelÀº ´Ù¸¥ Ŭ·¡½º¿¡µµ »ç¿ë °¡´ÉÇÑ Áßø Ŭ·¡½º¸¦ ¸¸µé ¶§ À¯¿ëÇÏ´Ù. ¼Ò½º ÆÄÀÏ¿¡¼­ Ŭ·¡½º À̸§À» °­Á¶ÇÏ°í(¶Ç´Â ¾Æ¿ô¶óÀÎ ºäÀÇ Å¬·¡½º À̸§À» Ŭ¸¯ÇÏ¿©) Refactor > Convert Nested Type to Top LevelÀ» ¼±ÅÃÇÑ´Ù.

ÀÌ ¸®ÆÑÅ丵Àº enclosing ÀνºÅϽº¿ë À̸§À» ¿äûÇÒ °ÍÀÌ´Ù. OK¸¦ ´©¸¥ ÈÄ enclosing BagExample Ŭ·¡½º¿ë ÄÚµå´Â º¯°æµÈ´Ù. (Listing 3).

Listing 3. ¸®ÆÑÅ丵ÀÌ ¼öÇàµÈ Bag Ŭ·¡½º

public class BagExample
{
   void processMessage(String msg)
   {
      Bag bag = new BagImpl(this);
      bag.set(msg);
      MessagePipe pipe = new MessagePipe();
      pipe.send(bag);
   }
}

Ŭ·¡½º°¡ ÁßøµÇ¸é ¿ÜºÎ Ŭ·¡½ºÀÇ ¸â¹ö¿¡ ´ëÇÑ ¾×¼¼½º¸¦ °¡Áø´Ù´Â °Í¿¡ ÁÖ¸ñÇ϶ó. ÀÌ ±â´ÉÀ» À¯ÁöÇϱâ À§ÇØ ¸®ÆÑÅ丵Àº enclosing Ŭ·¡½º BagExampleÀÇ ÀνºÅϽº¸¦ ÀÌÀü¿¡ ÁßøµÈ Ŭ·¡½º¿¡ Ãß°¡ ÇÒ °ÍÀÌ´Ù. ÀÌ°ÍÀº ÀÌÀü¿¡ À̸§À» Á¦°øÇϵµ·Ï ¿äû¹Þ¾Ò´ø ÀνºÅϽº º¯¼öÀÌ´Ù. ÀÌ°ÍÀº ¶ÇÇÑ ÀνºÅϽº º¯¼ö¸¦ ¼³Á¤ÇÏ´Â ÄÁ½ºÆ®·°Å͸¦ ¸¸µç´Ù. ´ÙÀ½Àº ¸®ÆÑÅ丵ÀÌ ¸¸µç ÀÌ »õ·Î¿î BagImpl Ŭ·¡½ºÀÌ´Ù. (Listing 4).

Listing 4. BagImpl Ŭ·¡½º

final class BagImpl implements Bag
{
   private final BagExample example;
   /**
    * @paramBagExample
    */
  BagImpl(BagExample example)
   {
      this.example = example;
      // TODO Auto-generated constructor stub
   }
   Object o;
   public Object get()
   {
      return o;
   }
   public void set(Object o)
   {
      this.o = o;
   }
}

BagExample Ŭ·¡½º¿¡ ´ëÇÑ ¾×¼¼½º¸¦ º¸ÀÏÇÊ¿ä°¡ ¾ø´Ù¸é ÀνºÅϽº º¯¼ö¿Í ÄÁ½ºÆ®·°Å͸¦ Á¦°ÅÇÏ°í BagExample Ŭ·¡½º¸¦ no-arg ÄÁ½ºÆ®·°ÅÍ·Î º¯°æÇÑ´Ù.

Ŭ·¡½º °èÃþ ³»¿¡¼­ ¸â¹ö ¿Å±â±â
µÎ °³ÀÇ ´Ù¸¥ ¸®ÆÑÅ丵(Push Down & Pull Up)Àº Ŭ·¡½º ¸Þ¼Òµå ¶Ç´Â Çʵ带 ÇϳªÀÇ Å¬·¡½º¿¡¼­ ÀÌ°ÍÀÇ ÇÏÀ§Å¬·¡½º ¶Ç´Â »óÀ§ Ŭ·¡½º·Î °¢°¢ À̵¿½ÃŲ´Ù. Ãß»ó Ŭ·¡½º VehicleÀÌ ÀÖ´Ù°í °¡Á¤Çغ¸ÀÚ. (Listing 5).

Listing 5. Vehicle Ãß»ó Ŭ·¡½º

public abstract class Vehicle
{
   protected int passengers;
   protected String motor;
   
   public int getPassengers()
   {
      return passengers;
   }
   public void setPassengers(int i)
   {
      passengers = i;
   }
   public String getMotor()
   {
      return motor;
   }
   public void setMotor(String string)
   {
      motor = string;
   }
}

AutomobileÀ̶ó´Â VehicleÀÇ ÇÏÀ§Å¬·¡½ºµµ ÀÖ´Ù. (Listing 6).

Listing 6. Automobile Ŭ·¡½º

public class Automobile extends Vehicle
{
   private String make;
   private String model;
   public String getMake()
   {
      return make;
   }
   public String getModel()
   {
      return model;
   }
   public void setMake(String string)
   {
      make = string;
   }
   public void setModel(String string)
   {
      model = string;
   }
}

VehicleÀÇ ¾ÖÆ®¸®ºäÆ® Áß Çϳª°¡ motor¶ó´Â °Í¿¡ ÁÖ¸ñÇÏÀÚ. ¸ðÅÍ·Î ±¸µ¿µÇ´Â Å»°Í ¸¸À» ´Ù·ê °ÍÀ̶ó¸é ±¦ÂúÁö¸¸ º¸Æ® °°Àº °Íµµ Çã¿ëÇÏ·Á¸é motor ¾ÖÆ®¸®ºäÆ®¸¦ Vehicle Ŭ·¡½º¿¡¼­ Automobile Ŭ·¡½º·Î ¹Ð¾î³»·Á¾ß ÇÑ´Ù. À̸¦ À§Çؼ­´Â Outline ºä¿¡¼­ motor¸¦ ¼±ÅÃÇÏ°í Refactor > Push DownÀ» ¼±ÅÃÇÑ´Ù.

Eclipse´Â ¿©·¯ºÐÀÌ Çʵ带 ¾ðÁ¦³ª ¿Å±æ ¼ö´Â ¾ø´Ù´Â °ÍÀ» ÀνÄÇÒ Á¤µµ·Î ¶È¶ÈÇϱ⠶§¹®¿¡ Add Required ¶ó´Â ¹öÆ°À» Á¦°øÇÑ´Ù. ÇÏÁö¸¸ Eclipse 2.1¿¡¼­´Â ¾ðÁ¦³ª Á¤È®ÇÏ°Ô ÀÛµ¿ÇÏ´Â °ÍÀº ¾Æ´Ï´Ù. ÀÌ Çʵ忡 ±Ù°ÅÇÏ´Â ¸ðµç ¸Þ¼Òµå°¡ "push down" µÇ¾ú´ÂÁö¸¦ È®ÀÎÇØ¾ß ÇÑ´Ù. ÀÌ °æ¿ì motor Çʵ带 ¼ö¹ÝÇÏ´Â getter¿Í setter ¸Þ¼Òµå°¡ ÀÖ´Ù. (±×¸² 3).

±×¸² 3. Add Required
Adding required members

OK¸¦ ´©¸¥ ÈÄ, motor Çʵå¿Í getMotor() & setMotor() ¸Þ¼Òµå´Â Automobile Ŭ·¡½º·Î À̵¿ÇÑ´Ù. Listing 7Àº Automobile Ŭ·¡½ºÀÇ ¸®ÆÑÅ丵 ÈÄÀÇ ¸ð½ÀÀÌ´Ù.

Listing 7. ¸®ÆÑÅ丵 ÈÄÀÇ Automobile Ŭ·¡½º

public class Automobile extends Vehicle
{
   private String make;
   private String model;
   protected String motor;
   public String getMake()
   {
      return make;
   }
   public String getModel()
   {
      return model;
   }
   public void setMake(String string)
   {
      make = string;
   }
   public void setModel(String string)
   {
      model = string;
   }
   public String getMotor()
   {
      return motor;
   }
   public void setMotor(String string)
   {
      motor = string;
   }
}

Pull Up ¸®ÆÑÅ丵Àº Push down°ú °ÅÀÇ µ¿ÀÏÇÏ´Ù. Ŭ·¡½º ¸â¹ö¸¦ Ŭ·¡½º¿¡¼­ »óÀ§Å¬·¡½º·Î ¿Å±ä´Ù´Â °ÍÀ» Á¦¿ÜÇϸé...

Automobile Ŭ·¡½º¿¡ motor°¡ ÀÖ´Ù´Â °ÍÀº Bus °°ÀÌ VehicleÀÇ ´Ù¸¥ ÇÏÀ§Å¬·¡½º¸¦ ¸¸µç´Ù¸é ¸ðÅ͸¦ Bus Ŭ·¡½º¿¡ Ãß°¡ÇؾßÇÑ´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù. ÀÌ¿Í °°Àº °ü°è¸¦ ³ªÅ¸³»´Â ÇÑ °¡Áö ¹æ¹ýÀº Motorized¶ó´Â ÀνºÅϽº¸¦ ¸¸µå´Â °ÍÀÌ´Ù. Automobile°ú Bus´Â ±¸ÇöÇÏÁö¸¸ RowBoat´Â ±×·¸Áö ¾Ê´Ù.

Motorized ÀÎÅÍÆäÀ̽º¸¦ ¸¸µå´Â °¡Àå ½¬¿î ¹æ¹ýÀº Automobile¿¡ Extract ÀÎÅÍÆäÀ̽º ¸®ÆÑÅ丵À» »ç¿ëÇÏ´Â °ÍÀÌ´Ù. Outline ºä¿¡¼­ Automobile Ŭ·¡½º¸¦ ¼±ÅÃÇÏ°í ¸Þ´º¿¡¼­ Refactor > Extract Interface¸¦ ¼±ÅÃÇÑ´Ù. (±×¸² 4).

±×¸² 4. Motorized ÀÎÅÍÆäÀ̽º
Motorized interface

OK¸¦ ¼±ÅÃÇÑ ÈÄ, ÀÎÅÍÆäÀ̽º°¡ ¸¸µé¾îÁø´Ù. (Listing 8).

Listing 8. Motorized ÀÎÅÍÆäÀ̽º

public interface Motorized
{
   public abstract String getMotor();
   public abstract void setMotor(String string);
}

Automobile¿ë Ŭ·¡½º ¼±¾ðÀº ´ÙÀ½°ú °°´Ù:


public class Automobile extends Vehicle implements Motorized

supertype »ç¿ëÇϱâ
ÀÌ ¹üÁÖ¿¡ Æ÷ÇÔµÈ ¸¶Áö¸· ¸®ÆÑÅ丵Àº Use Supertype Where PossibleÀÌ´Ù. ÀÚµ¿Â÷ÀÇ Àç°í¸¦ °ü¸®ÇÏ´Â ¾ÖÇø®ÄÉÀ̼ÇÀ» »ý°¢Çغ¸ÀÚ. ÀÌ°ÍÀº Automobile À¯ÇüÀÇ °´Ã¼¸¦ »ç¿ëÇÑ´Ù. ¸ðµç À¯ÇüÀÇ Å»°ÍÀ» ÇÚµéÇÏ·Á¸é ÀÌ ¸®ÆÑÅ丵À» »ç¿ëÇÏ¿© Automobile¿¡ ´ëÇÑ ·¹ÆÛ·±½º¸¦ º¯°æÇÒ ¼ö ÀÖ´Ù. (±×¸² 5). instanceof ¿ÀÆÛ·¹ÀÌÅ͸¦ »ç¿ëÇÏ¿© Äڵ忡¼­ ¸ðµç À¯Çü üŷÀ» ¼öÇàÇϸé ƯÁ¤ À¯ÇüÀ» »ç¿ëÇÏ´Â °ÍÀÌ ÀûÀýÇÑÁö ¶Ç´Â supertypeÀ» ¼±ÅÃÇÏ´Â °ÍÀÌ ÀûÀýÇÑÁö °áÁ¤ÇؾßÇÏ°í ù ¹ø° ¿É¼ÇÀ» üũÇÏ°Ô µÉ °ÍÀÌ´Ù. ´ë°Ô 'instanceof' ÀͽºÇÁ·¹¼Ç¿¡¼­ ¼±ÅÃµÈ supertypeÀ» »ç¿ëÇÑ´Ù.

±×¸² 5. AutomobileÀ» supertypeÀÎ Vehicle·Î ¹Ù²Ù±â
Supertype

supertypeÀÇ Çʿ伺Àº ÀÚ¹Ù ¾ð¾î¸¦ »ç¿ëÇÒ ¶§ Á¾Á¾ ¹ß»ýÇÑ´Ù. ƯÈ÷ Factory Method ÆÐÅÏÀÌ »ç¿ëµÉ ¶§ ±×·¸´Ù. ÀϹÝÀûÀ¸·Î ÀÌ°ÍÀº Ãß»ó Ŭ·¡½º¸¦ ±¸ÇöÇÏ´Â ±¸Ã¼Àû °´Ã¼¸¦ ¸®ÅÏÇÏ´Â Á¤Àû create() ¸Þ¼Òµå¸¦ °®°í ÀÖ´Â Ãß»ó Ŭ·¡½º¸¦ °®Ã߸鼭 ±¸ÇöµÈ´Ù. ±¸ÇöµÇ¾î¾ß ÇÒ ±¸Ã¼Àû °´Ã¼ÀÇ À¯ÇüÀÌ Å¬¶óÀ̾ðÆ® Ŭ·¡½º¿Í °ü°è°¡ ¾ø´Â ±¸Çö »ó¼¼¿¡ ÀÇÁ¸ÇÑ´Ù¸é À¯¿ëÇÏ°Ô ¾²ÀδÙ.

Ŭ·¡½º ³»ºÎ¿¡¼­ ÄÚµå º¯°æÇϱâ
°¡Àå ±¤¹üÀ§ÇÑ ¸®ÆÑÅ丵Àº Ŭ·¡½º ³»¿¡¼­ Äڵ带 ÀνÄÇÏ´Â ¸®ÆÑÅ丵ÀÌ´Ù. ¹«¾ùº¸´Ùµµ À̵éÀº Áß°£ º¯¼ö¸¦ °¡Á®¿À°í ¿À·¡µÈ ¸Þ¼ÒµåÀÇ ÇÑ ºÎºÐ¿¡¼­ »õ ¸Þ¼Òµå¸¦ ¸¸µé°í Çʵå¿ë setter ¹× getter¸¦ ¸¸µç´Ù.

Extracting & inlining
Extract: Extract Method, Extract Local Variable, Extract Constants·Î ½ÃÀÛÇÏ´Â ¸¹Àº ¸®ÆÑÅ丵ÀÌ ÀÖ´Ù. Extract Method´Â ¿©·¯ºÐÀÌ ¼±ÅÃÇÑ Äڵ忡¼­ »õ·Î¿î ¸Þ¼Òµå¸¦ ¸¸µç´Ù. Ŭ·¡½ºÀÇ main() ¸Þ¼Òµå ¿¹¸¦ µé¾îº¸ÀÚ(Listing 8). ÀÌ°ÍÀº ¸í·ÉÇà ¿É¼ÇÀ» °è»êÇÏ°í -D·Î ½ÃÀÛÇÏ´Â ¹«¾ùÀΰ¡¸¦ ¹ß°ßÇϸé Properties °´Ã¼¿¡ À̸§ °ª ½ÖÀ¸·Î ±×µéÀ» ÀúÀåÇÑ´Ù.

Listing 8. main()

import java.util.Properties;
import java.util.StringTokenizer;
public class StartApp
{
   public static void main(String[] args)
   {
      Properties props = new Properties();
      for (int i= 0; i < args.length; i++)
      {
         if(args[i].startsWith("-D"))
         {
           String s = args[i].substring(2);
           StringTokenizer st = new StringTokenizer(s, "=");
            if(st.countTokens() == 2)
            {
              props.setProperty(st.nextToken(), st.nextToken());
            }
         }
      }
      //continue...
   }
}

¸Þ¼Òµå¿¡¼­ ¸î °³ÀÇ Äڵ带 °¡Á®´Ù°¡ À̸¦ ¶Ç ´Ù¸¥ ¸Þ¼Òµå¿¡ ³Ö´Â °÷¿¡ µÎ °¡Áö °æ¿ì°¡ ÀÖ´Ù. ù ¹ø° °æ¿ì´Â ¸Þ¼Òµå°¡ ³Ê¹« ±æ°í µÎ °³ ÀÌ»óÀÇ ±¸º°µÈ ³í¸®Àû ÀÛµ¿À» ¼öÇàÇÑ´Ù. µÎ ¹ø° °æ¿ì´Â ´Ù¸¥ ¸Þ¼Òµå¿¡ ÀÇÇØ Àç»ç¿ë µÉ ¼ö ÀÖ´Â ÄÚµåÀÇ ±¸º°µÈ ¼½¼ÇÀÌ ÀÖ´Ù¸é µÎ ¹ø° ÄÉÀ̽ºÀÌ´Ù.

À̸§ °ª ½ÖÀ» ÆĽÌÇÏ°í ±×µéÀ» Properties °´Ã¼¿¡ Ãß°¡ÇؾßÇÏ´Â ¶Ç ´Ù¸¥ Àå¼Ò°¡ ÀÖ°í StringTokenizer ¼±¾ð°ú µÚµû¸£´Â if ÀýÀ» Æ÷ÇÔÇÏ´Â ÄÚµå »ö¼ÇÀ» ÃßÃâÇÒ ¼ö ÀÖ´Ù°í °¡Á¤Çغ¸ÀÚ. À̸¦ ¼öÇàÇϱâ À§Çؼ­´Â ÀÌ Äڵ带 °­Á¶ÇÏ°í ±×·±´ÙÀ½ Refactor > Extract Method¸¦ ¸Þ´º¿¡¼­ ¼±ÅÃÇÑ´Ù. ¸Þ¼Òµå À̸§ÀÌ ÇÁ·ÒÇÁÆ®µÇ¸é addProperty¸¦ ÀÔ·ÂÇÏ°í ÀÌ ¸Þ¼Òµå°¡ µÎ °³ÀÇ ¸Å°³º¯¼ö(Properties prop & Strings)¸¦ °®°í ÀÖÀ½À» È®ÀÎÇÑ´Ù. (Listing 9).

Listing 9. ÃßÃâµÈ addProp()

import java.util.Properties;
import java.util.StringTokenizer;
public class Extract
{
   public static void main(String[] args)
   {
      Properties props = new Properties();
      for (int i = 0; i < args.length; i++)
      {
         if (args[i].startsWith("-D"))
         {
            String s = args[i].substring(2);
            addProp(props, s);
         }
      }
   }
   private static void addProp(Properties props, String s)
   {
      StringTokenizer st = new StringTokenizer(s, "=");
      if (st.countTokens() == 2)
      {
         props.setProperty(st.nextToken(), st.nextToken());
      }
   }
}

Extract Local Variable ¸®ÆÑÅ丵Àº Á÷Á¢ »ç¿ëµÇ°í ·ÎÄà º¯¼ö¿¡ ¸ÕÀú ÇÒ´çµÇ´Â ÀͽºÇÁ·¹¼ÇÀ» ÃëÇÑ´Ù. ÀÌ º¯¼ö´Â ÀͽºÇÁ·¹¼ÇÀÌ ÀÖ¾ú´ø °÷¿¡¼­ »ç¿ëµÈ´Ù. º¯¼ö¸¦ Á¦°øÇ϶ó´Â ÇÁ·ÒÇÁÆ®°¡ ¶ß¸é key¸¦ ÀÔ·ÂÇÑ´Ù. ¼±ÅÃµÈ ¸ðµç ÀͽºÇÁ·¹¼ÇÀ» »õ·Î¿î º¯¼ö¿¡ ´ëÇÑ ·¹ÆÛ·±½º·Î ´ëüÇÏ´Â ¿É¼ÇÀÌ ÀÖ´Ù. ÀÌ°ÍÀº Á¾Á¾ ÀûÀýÇÏÁö¸¸ nextToken() ¸Þ¼ÒµåÀÇ °æ¿ì¿¡´Â ±×·¸Áö ¾Ê´Ù. È£ÃâµÉ ¶§¸¶´Ù ¸Å¹ø ´Ù¸¥ °ªÀ» ¸®ÅÏÇÑ´Ù.ÀÌ ¿É¼Ç¿¡´Â ¼±ÅõÇÁö ¾Ê¾Ò´Ù. (±×¸² 6).

±×¸² 6. ¼±ÅÃµÈ ¸ðµç ÀͽºÇÁ·¹¼ÇÀ» ´ëüÇÏÁö ¾Ê´Â´Ù.
Extract variable

±×·± ´ÙÀ½ st.nextToken()¿¡ ´ëÇÑ µÎ ¹ø° È£Ãâ¿¡ ´ëÇØ ÀÌ ¸®ÆÑÅ丵À» ¹Ýº¹ÇÑ´Ù. À̹ø¿¡´Â »õ·Î¿î ·ÎÄà º¯¼ö value¸¦ È£ÃâÇÑ´Ù. Listing 10Àº µÎ °³ÀÇ ¸®ÆÑÅ丵 ÈÄÀÇ ÄÚµå ¸ð½ÀÀÌ´Ù .

Listing 10. ¸®ÆÑÅ丵 ÈÄÀÇ ÄÚµå

private static void addProp(Properties props, String s)
   {
     StringTokenizer st = new StringTokenizer(s, "=");
      if(st.countTokens() == 2)
      {
         String key = st.nextToken();
         String value = st.nextToken();
        props.setProperty(key, value);
      }
   }

ÀÌ·¯ÇÑ ¹æ½ÄÀ¸·Î º¯¼ö¸¦ µµÀÔÇÒ ¶§ ¿©·¯ ÀÌÁ¡ÀÌ ÀÖ´Ù. ¿ì¼± ÀͽºÇÁ·¹¼Ç¿¡ Àǹ̰¡ dzºÎÇÑ À̸§À» Á¦°øÇÔÀ¸·Î¼­ Äڵ尡 ¹«¾ùÀ» ¼öÇàÇÏ´ÂÁö°¡ ºÐ¸íÇØÁø´Ù. µÑ°·Î, ÀͽºÇÁ·¹¼ÇÀÌ ¸®ÅÏÇÏ´Â °ªÀ» ½±°Ô °Ë»çÇÒ ¼ö Àֱ⠶§¹®¿¡ ÄÚµå µð¹ö±ëÀÌ ´õ¿í ½±´Ù. ¸¶Áö¸·À¸·Î ÀͽºÇÁ·¹¼ÇÀÇ ¸¹Àº ÀνºÅϽºµéÀÌ ÇϳªÀÇ º¯¼ö·Î ´ëüµÉ ¼ö ÀÖ´Â °æ¿ì ´õ¿í È¿À²ÀûÀÌ´Ù.

Extract Constant´Â Extract Local Variable¿Í ºñ½ÁÇÏÁö¸¸ Á¤Àû »ó¼ö ÀͽºÇÁ·¹¼ÇÀ» ¼±ÅÃÇؾßÇÑ´Ù. ÀÌ°ÍÀº Çϵå ÄÚµùµÈ ¼ýÀÚ¿Í ½ºÆ®¸µÀ» Äڵ忡¼­ Á¦°ÅÇϴµ¥ À¯¿ëÇÏ´Ù. ¿¹¸¦ µé¾î, À§ Äڵ忡¼­ À̸§ °ª ½ÖÀ» Á¤ÀÇÇϸ鼭 ¸í·ÉÇà ¿É¼ÇÀ» À§ÇØ -D" ¸¦ »ç¿ëÇß´Ù. Äڵ忡¼­ -D" ¸¦ ÇÏÀ̶óÀÌÆ® ÇÏ°í Refactor > Extract Constant¸¦ ¼±ÅÃÇÑ ´ÙÀ½ »ó¼ö À̸§À¸·Î DEFINEÀ» ÀÔ·ÂÇÑ´Ù. ÀÌ ¸®ÆÑÅ丵Àº Äڵ带 º¯°æÇÒ °ÍÀÌ´Ù. (Listing 11).

Listing 11. ¸®ÆÑÅ丵µÈ ÄÚµå

public class Extract
{
   private static final String DEFINE = "-D";
   public static void main(String[] args)
   {
      Properties props = new Properties();
      for (int i = 0; i < args.length; i++)
      {
         if (args[i].startsWith(DEFINE))
         {
            String s = args[i].substring(2);
            addProp(props, s);
         }
      }
   }
   // ...

°¢ Extract... ¸®ÆÑÅ丵ÀÇ °æ¿ì »óÀÀÇÏ´Â ÀζóÀÎ ... refactoringÀÌ ÀÖ¾î ¹Ý´ë ÀÛµ¿À» ¼öÇàÇÑ´Ù. ¿¹¸¦ µé¾î À§ ÄÚµåÀÇ º¯¼ö s¸¦ °­Á¶ÇÏ°í Refactor > Inline...¸¦ ¼±ÅÃÇÏ°í OK¸¦ ´©¸£¸é Eclipse´Â addProp()¿¡ ´ëÇÑ È£Ãâ¿¡ Á÷Á¢ args[i].substring(2) ÀͽºÇÁ·¹¼ÇÀ» »ç¿ëÇÑ´Ù:


        if(args[i].startsWith(DEFINE))
         {
            addProp(props,args[i].substring(2));
         }

ÇʵåÀÇ Ä¸½¶È­
°´Ã¼ÀÇ ³»ºÎ ±¸Á¶¸¦ ³ëÃâÇÏ´Â °ÍÀº ÁÁÀº °ü·Ê·Î ¿©°ÜÁöÁö ¾Ê¾Ò´Ù. Vehicle Ŭ·¡½º¿Í ÀÌ°ÍÀÇ ÇÏÀ§Å¬·¡½º°¡ ÇÁ¶óÀ̺ø ÇÊµå ¶Ç´Â º¸È£ Çʵ带 °®°í ÀÖ°í ÆÛºí¸¯ setter¿Í getter ¸Þ¼Òµå°¡ ¾×¼¼½º¸¦ Á¦°øÇÏ´Â ÀÌÀ¯µµ ±×°ÍÀÌ´Ù. ÀÌ·¯ÇÑ ¸Þ¼ÒµåµéÀº µÎ °¡Áö ´Ù¸¥ ¹æ¹ýÀ¸·Î ¸¸µé¾îÁø´Ù.

ÀÌ·¯ÇÑ ¸Þ¼ÒµåµéÀ» ¸¸µå´Â ÇÑ °¡Áö ¹æ¹ýÀº Source > Generate Getter and Setter¸¦ »ç¿ëÇÏ´Â °ÍÀÌ´Ù. °¢°¢ÀÇ Çʵ尡 °®°í ÀÖÁö ¾ÊÀº Á¦¾ÈµÈ getter¿Í setter ¸Þ¼Òµå°¡ ÀÖ´Â ´ÙÀ̾ó·Î±× ¹Ú½º¸¦ µð½ºÇ÷¹ÀÌ ÇÒ °ÍÀÌ´Ù. ÀÌ°ÍÀº ¸®ÆÑÅ丵Àº ¾Æ´Ï´Ù. »õ·Î¿î ¸Þ¼Òµå¸¦ »ç¿ëÇϱâ À§ÇØ Çʵ忡 ´ëÇÑ ·¹ÆÛ·±½º¸¦ ¾÷µ¥ÀÌÆ® ÇÏÁö ¾Ê±â ¶§¹®ÀÌ´Ù. ÀÌ ¿É¼ÇÀº ½Ã°£ Àý¾à¿¡´Â È¿°ú°¡ ÀÖÁö¸¸ Ŭ·¡½º¸¦ óÀ½ ¸¸µé ¶§ °¡Àå ÀûÇÕÇÏ´Ù. ¶Ç´Â »õ·Î¿î Çʵ带 Ŭ·¡½º¿¡ Ãß°¡ÇÒ ¶§µµ ÀûÀýÇÏ´Ù.

getter¿Í setter ¸Þ¼Òµå¸¦ ¸¸µå´Â µÎ ¹ø° ¹æ¹ýÀº Çʵ带 ¼±ÅÃÇÏ°í ¸Þ´º¿¡¼­ Refactor > Encapsulate Field¸¦ ¼±ÅÃÇÏ´Â °ÍÀÌ´Ù. ÀÌ ¸Þ¼Òµå´Â Çѹø¿¡ ÇÑ Çʵ忡 ´ëÇÑ getter¿Í setter¸¦ ¸¸µéÁö¸¸ Source > Generate Getter and Setter¿Í´Â ´Þ¸® Çʵ忡 ´ëÇÑ ·¹ÆÛ·±½º¸¦ »õ·Î¿î ¸Þ¼Òµå¿¡ ´ëÇÑ È£Ãâ·Î º¯°æÇÑ´Ù.

»õ·Î¿î Automobile Ŭ·¡½º·Î ½ÃÀÛÇغ¸ÀÚ (Listing 12).

Listing 12. Automobile Ŭ·¡½º

public class Automobile extends Vehicle
{
   public String make;
   public String model;
}

AutomobileÀ» ÀνºÅϽºÈ­Çϴ Ŭ·¡½º¸¦ ¸¸µé°í make Çʵ忡 Á÷Á¢ ¾×¼¼½ºÇÑ´Ù. (Listing 13).

Listing 13. AutomobileÀÇ ÀνºÅϽºÈ­

public class AutomobileTest
{
   public void race()
   {
      Automobilecar1 = new Automobile();
      car1.make= "Austin Healy";
      car1.model= "Sprite";
      // ...
   }
}

Çʵå À̸§À» °­Á¶ÇÏ°í Refactor > Encapsulate Field¸¦ ¼±ÅÃÇÏ¿© make Çʵ带 ĸ½¶È­ÇÑ´Ù. ´ÙÀ̾ó·Î±×¿¡¼­ getter¿Í setter ¸Þ¼Òµå¿ë À̸§À» ÀÔ·ÂÇÑ´Ù. ¿¹»óÇß°ÚÁö¸¸ ÀÌ´Â ±âº»ÀûÀ¸·Î getMake()¿Í setMake() ÀÌ´Ù. Çʵå¿Í °°Àº Ŭ·¡½º¿¡ ÀÖ´Â ¸Þ¼Òµå°¡ Çʵ忡 Á÷Á¢ ¾×¼¼½º ÇÒ °ÍÀÎÁö ¶Ç´Â ÀÌ ·¹ÆÛ·±½º°¡ º¯°æµÇ¾î ¸ðµç ´Ù¸¥ Ŭ·¡½º ó·³ ¾×¼¼½º ¸Þ¼Òµå¸¦ »ç¿ëÇÒ °ÍÀÎÁö¸¦ ¼±ÅÃÇÑ´Ù. (±×¸² 7).

±×¸² 7. ÇʵåÀÇ Ä¸½¶È­
Encapsulating a field

OK¸¦ ´©¸¥ ÈÄ, Automobile Ŭ·¡½º¿¡ ÀÖ´Â make Çʵå´Â ÇÁ¶óÀ̺øÀÌ µÇ°í getMake()¿Í setMake() ¸Þ¼Òµå¸¦ °®°ÔµÈ´Ù. (Listing 14).

Listing 14. ¸®ÆÑÅ丵 ÈÄÀÇ Automobile Ŭ·¡½º

public class Automobile extends Vehicle
{
   private String make;
   public String model;

   public void setMake(String make)
   {
      this.make = make;
   }

   public String getMake()
   {
      return make;
   }
}

AutomobileTest Ŭ·¡½º´Â ¾÷µ¥ÀÌÆ® µÇ¾î »õ·Î¿î ¾×¼¼½º ¸Þ¼Òµå¸¦ »ç¿ëÇÑ´Ù. (Listing 15).

Listing 15. AutomobileTest Ŭ·¡½º

public class AutomobileTest
{
   public void race()
   {
      Automobilecar1 = new Automobile();
      car1.setMake("Austin Healy");
      car1.model= "Sprite";
      // ...
   }
}

Change Method Signature
¸¶Áö¸· ¸®ÆÑÅ丵(Change Method Signature)Àº »ç¿ëÇϱ⠰¡Àå ¾î·Æ´Ù. ÀÌ°ÍÀÌ ¹«¾ùÀ» ¼öÇàÇÏ´ÂÁö´Â ¸Å¿ì ¸íÈ®ÇÏ´Ù. ¸Å°³º¯¼ö, °¡½Ã¼º, ¸Þ¼ÒµåÀÇ ¸®ÅÏ Å¸ÀÔÀ» º¯°æÇÑ´Ù. ºÐ¸íÇÏÁö ¾ÊÀº °ÍÀº ÀÌ·¯ÇÑ º¯°æÀÌ ¸Þ¼Òµå ¶Ç´Â ±× ¸Þ¼Òµå¸¦ È£ÃâÇÏ´Â Äڵ忡 ´ëÇÑ ¿µÇâÀÌ´Ù. ¿©±â¿¡ ¸¶¼úÀº Á¸ÀçÇÏÁö ¾Ê´Â´Ù. ¸®ÆÑÅ丵µÇ´Â ¸Þ¼Òµå¿¡¼­ º¯°æÀ¸·Î ÀÎÇØ ¹®Á¦°¡ »ý±ä´Ù¸é ¸®ÆÑÅ丵 ÀÛµ¿Àº À̸¦ °ÅºÎÇÑ´Ù. ¾î·µç ¸®ÆÑÅ丵À» ¼ö¶ôÇÏ¿© ³ªÁß¿¡ ¹®Á¦¸¦ Á¤Á¤Çϰųª ¸®ÆÑÅ丵À» Ãë¼ÒÇØ¾ß ÇÑ´Ù. ¸®ÆÑÅ丵ÀÌ ´Ù¸¥ ¸Þ¼Òµå¿¡¼­ ¹®Á¦¸¦ ÀÏÀ¸Å°¸é ÀÌ°ÍÀº ¹«½ÃµÇ°í ¸®ÆÑÅ丵 ÈÄ¿¡ ½º½º·Î ¹®Á¦¸¦ ÇØ°áÇØ¾ß ÇÑ´Ù.

Listing 16. MethodSigExample Ŭ·¡½º

public class MethodSigExample
{
   public int test(String s, int i)
   {
      int x = i + s.length();
      return x;
   }
}

test() ¸Þ¼Òµå´Â ´Ù¸¥ Ŭ·¡½ºÀÇ ¸Þ¼Òµå¿¡ ÀÇÇØ È£ÃâµÈ´Ù. (Listing 17).

Listing 17. callTest ¸Þ¼Òµå

public void callTest()
   {
     MethodSigExample eg = new MethodSigExample();
     int r = eg.test("hello", 10);
   }

ù ¹ø° Ŭ·¡½º¿¡¼­ test¸¦ ÇÏÀ̶óÀÌÆÃÇÏ°í Refactor > Change Method Signature¸¦ ¼±ÅÃÇÑ´Ù. ´ÙÀ̾ó·Î±× ¹Ú½º°¡ ³ªÅ¸³­´Ù. (±×¸² 8).

±×¸² 8. Change Method Signature ¿É¼Ç
Change Method Signature options

ÀÌ Ã¹ ¹ø° ¿É¼ÇÀº ¸Þ¼ÒµåÀÇ °¡½Ã¼ºÀ» º¯°æÇÏ´Â °ÍÀÌ´Ù. ÀÌ ¿¹Á¦¿¡¼­ À̸¦ protected ¶Ç´Â privateÀ¸·Î º¯°æÇÏ¿© µÎ ¹ø° Ŭ·¡½ºÀÇ callTest() ¸Þ¼Òµå°¡ Á¢±ÙÇÒ ¼ö ¾øµµ·Ï ÇÑ´Ù. Eclipse´Â ¸®ÆÑÅ丵À» ½ÇÇàÇÏ´Â µ¿¾È ÀÌ ¿¡·¯¸¦ Á¤ÁöÇÏÁö ¾Ê´Â´Ù. ÀûÀýÇÑ °ªÀ» ¼±ÅÃÇÏ´Â °ÍÀº ¿©·¯ºÐÀÇ ¸òÀÌ´Ù.

´ÙÀ½ ¿É¼ÇÀº ¸®ÅÏ À¯ÇüÀ» º¯°æÇÏ´Â °ÍÀÌ´Ù. ¿¹¸¦ µé¾î ¸®ÅÏ À¯ÇüÀ» float·Î º¯°æÇÑ´Ù°í Çؼ­ ¿¡·¯¸¦ Á¤ÁöÇÒ ¼ö ¾ø´Ù. test() ¸Þ¼ÒµåÀÇ ¸®ÅÏ ¹®ÀÇ int°¡ ÀÚµ¿À¸·Î floatÀ» ÇÁ·ÒÇÁÆ®Çϱ⠶§¹®ÀÌ´Ù. ±×·³¿¡µµ ÀÌ°ÍÀº µÎ ¹ø° Ŭ·¡½ºÀÇ callTest()¿¡¼­ ¹®Á¦¸¦ ÀÏÀ¸Å²´Ù. floatÀÌ int·Î º¯È¯µÉ ¼ö ¾ø±â ¶§¹®ÀÌ´Ù. test()¿¡ ÀÇÇØ ¸®ÅÏµÈ ¸®ÅÏ °ªÀ» int¿¡ ij½ºÆÃÇϰųª callTest()ÀÇ r ŸÀÔÀ» floatÀ¸·Î º¯°æÇÑ´Ù.

String¿¡¼­ intÀ¸·Î ù ¹ø° ¸Å°³º¯¼ö À¯ÇüÀ» º¯°æÇÑ´Ù¸é ºñ½ÁÇÑ °ÍÀÌ °í·ÁµÉ ¼ö ÀÖ´Ù. ÀÌ°ÍÀº ¸®ÆÑÅ丵 ½ÇÇà Áß Á¤ÁöµÈ´Ù. ¸®ÆÑÅ丵 µÇ°íÀÖ´Â ¸Þ¼Òµå¿¡ ¹®Á¦¸¦ ÀÏÀ¸Å°±â ¶§¹®ÀÌ´Ù: intÀº length() ¸Þ¼Òµå¸¦ °®°í ÀÖÁö ¾Ê´Ù. À̸¦ StringBuffer·Î ¹Ù²Ù¸é ¹®Á¦·Î ÀÎÇØ Á¤ÁöµÇÁö ¾Ê´Â´Ù. length() ¸Þ¼Òµå°¡ ¾ø±â ¶§¹®ÀÌ´Ù. ¹°·Ð ÀÌ°Í ¿ª½Ã callTest() ¸Þ¼Òµå¿¡ ¹®Á¦¸¦ ÀÏÀ¸Å²´Ù. test() ¸¦ È£ÃâÇÒ ¶§ StringÀ» Àü´ÞÇϱ⠶§¹®ÀÌ´Ù.

¾Õ¼­ ¾ð±ÞÇßÁö¸¸ ¸®ÆÑÅ丵ÀÌ ¿¡·¯¶ó´Â °á°ú¸¦ ³»´Â °æ¿ì, Á¤Áö µÇµç ¾ÈµÇµç °æ¿ì¿¡ µû¶ó ¿¡·¯¸¦ Á¤Á¤ÇÏ¿© Áö¼ÓÇÒ ¼ö ÀÖ´Ù. ¶Ç´Ù¸¥ ¹æ¹ýÀ¸·Î´Â ¿¡·¯¸¦ »çÀüÁ¡À¯ÇÏ´Â °ÍÀÌ´Ù. i ¸Å°³º¯¼ö¸¦ Á¦°ÅÇÏ·Á¸é ¸®ÆÑÅ丵 µÇ°íÀÖ´Â ¸Þ¼Òµå¿¡¼­ ÀÌ¿¡ ´ëÇÑ ·¹ÆÛ·±½º¸¦ Á¦°ÅÇÒ ¼ö ÀÖ´Ù. ÀÌ ¸Å°³º¯¼ö¸¦ Á¦°ÅÇÏ¸é ´õ¿í ºÎµå·¯¿ö Áø´Ù.

Default Value ¿É¼ÇÀ» ¸¶Áö¸·À¸·Î ¼³¸íÇÏ°Ú´Ù. ÀÌ°ÍÀº ¸Å°³º¯¼ö°¡ ¸Þ¼Òµå ½ÅÈ£¿¡ Ãß°¡µÉ ¶§¿¡¸¸ »ç¿ëµÈ´Ù. ¸Å°³º¯¼ö°¡ ÄÝ·¯¿¡ Ãß°¡µÉ ¶§ °ªÀ» Á¦°øÇϴµ¥ »ç¿ëµÈ´Ù. ¿¹¸¦ µé¾î nÀ̶ó´Â À̸§À» °¡Áø String ŸÀÔÀÇ ¸Å°³º¯¼ö¿Í worldÀÇ µðÆúÆ® °ªÀ» callTest() ¸Þ¼ÒµåÀÇ test()¿¡ ´ëÇÑ È£Ãâ¿¡ Ãß°¡ÇÏ¸é ´ÙÀ½°ú °°ÀÌ µÈ´Ù :


   public void callTest()
   {
      MethodSigExample eg = new MethodSigExample();
      int r = eg.test("hello", 10, "world");
   }

¿ä¾à
EclipseÀÇ ÅøÀ» »ç¿ëÇÏ¸é ¸®ÆÑÅ丵ÀÌ ½±´Ù. ÀÌ Åø¿¡ Àͼ÷ÇØÁö¸é »ý»ê¼ºµµ Çâ»óµÈ´Ù. ÇÁ·Î±×·¥ ±â´ÉÀ» ¹Ýº¹ÀûÀ¸·Î Ãß°¡ÇÏ´Â °³¹ß ¹æ½Ä¿¡¼­´Â ÇÁ·Î±×·¥ µðÀÚÀÎÀÇ º¯°æ ¹× È®Àå ±â¼ú·Î¼­ ¸®ÆÑÅ丵¿¡ ÀÇÁ¸ÇÑ´Ù. ÇÏÁö¸¸ ¸®ÆÑÅ丵¿¡¼­ ¿ä±¸ÇÏ´Â °ø½ÄÀû ¹æ¹ýÀ» »ç¿ëÇÏÁö ¾Ê´õ¶óµµ EclipseÀÇ ¸®ÆÑÅ丵 ÅøÀº ÀϹÝÀûÀÎ ÄÚµå º¯°æÀ» °¡´ÉÇÏ°Ô ÇÏ´Â ¹æ½ÄÀ» Á¦°øÇÑ´Ù.

Âü°íÀÚ·á

Ã¥

  • Refactoring: Improving the Design of Existing Code by Martin Fowler, Kent Beck, John Brant, William Opdyke, and Don Roberts (Addison-Wesley, 1999).

  • Eclipse In Action: A Guide for Java Developers, by David Gallardo, Ed Burnette, and Robert McGovern (Manning, 2003).

  • Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison-Wesley, 1995).

  • Design Patterns use C++; for a book that translates patterns to the Java language, see Patterns in Java, Volume One: A Catalog of Reusable Design Patterns Illustrated with UML, Mark Grand (Wiley, 1998).

  • Extreme Programming Explained: Embrace Change, by Kent Beck (Addison-Wesley, 1999).

À¥ »çÀÌÆ®

developerWorks Æ©Å丮¾ó

¸ñ Â÷:
¸®ÆÑÅ丵
EclipseÀÇ ¸®ÆÑÅ丵 À¯Çü
¹°¸®Àû À籸¼º(reorganization)°ú Àç¸í¸í(renaming)
Ŭ·¡½º °ü°è ÀçÁ¤ÀÇ
Ŭ·¡½º ³»ºÎ¿¡¼­ ÄÚµå º¯°æÇϱâ
¿ä¾à
Âü°í ÀÚ·á
ÇÊÀÚ ¼Ò°³
±â»ç¿¡ ´ëÇÑ Æò°¡
°ü·Ã dW ¸µÅ©:
Refactoring with Eclipse
Java design patterns 101
Java design patterns 201
Extend Eclipse's Java Development Tools
Eclipse Platform ½ÃÀÛÇϱâ
All Eclipse articles on developerWorks
Subscribe to the developerWorks newsletter
US ¿ø¹® Àбâ
Also in the Java zone:
Tutorials
Tools and products
Code and components
Articles
ÇÊÀÚ¼Ò°³
David Gallardo´Â ¼ÒÇÁÆ®¿þ¾î ÄÁ¼³ÅÏÆ®ÀÌÀÚ ¼ÒÇÁÆ®¿þ¾î ±¹Á¦È­, ÀÚ¹Ù À¥ ¾ÖÇø®ÄÉÀ̼Ç, µ¥ÀÌÅͺ£À̽º °³¹ß Àü¹® ÀÛ°¡ÀÌ´Ù.
ÀÌ ±â»ç¿¡ ´ëÇÏ¿© ¾î¶»°Ô »ý°¢ÇϽʴϱî?

Á¤¸» ÁÁ´Ù (5) ÁÁ´Ù (4) ±×Àú±×·¸´Ù (3) ¼öÁ¤º¸¿ÏÀÌ ÇÊ¿äÇÏ´Ù(2) ÇüÆí¾ø´Ù (1)

  È¸»ç¼Ò°³  |  °³ÀÎÁ¤º¸ º¸È£Á¤Ã¥  |  ¹ý·ü  |  ¹®ÀÇ