Java reflectionÀ» ÀÌ¿ëÇÏ¿© º¸´Ù ÀϹÝÀûÀÎ business delegate
±¸ÇöÇϱâ
Brett
McLaughlin ÀÛ°¡/ÆíÁýÀÚ, O'Reilly and Associates 2002³â
11¿ù
business delegate Ŭ·¡½º´Â ¿£ÅÍÇÁ¶óÀÌÁî
ÀÚ¹Ù µðÀÚÀο¡ ³î¶ó¿î À¯¿¬¼ºÀ» ºÎ¿©ÇßÁö¸¸ ¾ÖÇø®ÄÉÀ̼ÇÀÇ ¸ðµç ¼¼¼Ç¿¡ business delegate¸¦
ÄÚµùÇÏ´Â ÀÏÀº ¿©°£ ¼º°¡½Å ÀÏÀÌ ¾Æ´Ò ¼ö ¾ø´Ù. ÀÌ ±ÛÀ» ÅëÇØ, Á»´õ ÀϹÝÀûÀÎ ¹öÀüÀÇ business
delegate Ŭ·¡½ºÀÎ µ¿Àû delegate¸¦ ¸¸µå´Â ¹æ¹ýÀ» ¹è¿öº¸ÀÚ.
ÀÌÀü ±Û ¿¡¼, business delegate Ŭ·¡½º(Business
Interface ÆÐÅÏ°ú È¥µ¿ÇÏÁö ¸» °Í!)¸¦ »ç¿ëÇÏ¿© EJB ÄÄÆ÷³ÍÆ®¿¡ ¾×¼¼½º ÇÏ´Â ¹æ¹ýÀ» À̾߱âÇß´Ù.
business delegate Ŭ·¡½º¸¦ Ŭ¶óÀ̾ðÆ®¿Í EJB ÄÚµå »çÀÌ¿¡ »ðÀÔÇÔÀ¸·Î¼ EJB ±¸Á¶¿Í ºñÁö´Ï½º
·ÎÁ÷¿¡¼ ¾ÖÇø®ÄÉÀ̼ÇÀÇ À¥ Ƽ¾î¸¦ ºÐ¸®ÇÒ ¼ö ÀÖ¾ú´Ù.
ÀÌ·¯ÇÑ À¯ÇüÀÇ µðÀÚÀÎÀ» º¸´Â ÇÑ °¡Áö ¹æ¹ýÀº ÀÌ°ÍÀÌ ¾ó¸¶³ª ÀϹÝÀûÀΰ¡ ÇÏ´Â °üÁ¡¿¡¼ º¸´Â °ÍÀÌ´Ù. ºñÁö´Ï½º
·ÎÁ÷°ú ±â¼úÀû ±â´ÉÀÌ ±ä¹ÐÇÏ°Ô ¿¬°áµÈ ¾ÖÇø®ÄÉÀ̼ÇÀ» ½ÃÀÛÇÏ¸é¼ ¿ì¸®´Â Á¡Â÷ÀûÀ¸·Î ´Ù¸¥ ·¹À̾îµéÀÇ ¾ÖÇø®ÄÉÀ̼ÇÀ»
ºÐ¸®ÇÏ°í ±×µéÀÇ »óÈ£ÀÇÁ¸¼ºÀ» ¿ÏȽÃÅ°±â À§ÇØ ´Ù¾çÇÑ ±â¼úÀ» »ç¿ëÇϱ⠽ÃÀÛÇß´Ù. ÀÌ·¸°Ô ÇÔÀ¸·Î¼ ¾ÖÇø®ÄÉÀ̼ÇÀÇ
±â¹Ý ±¸Á¶°¡ ÀϹÝÀûÀϼö·Ï ½Ã°£ÀÌ È帣¸é¼ °á±¹ ´õ¿í °·ÂÇÏ°í °ü¸®°¡ ½¬¿öÁø´Ù´Â °ÍÀ» ¾Ë°Ô µÈ´Ù.
ÀÌ ±Û¿¡¼ ÀϹÝÀûÀÎ µðÀÚÀÎ À̶ó´Â °³³äÀ» À̾ °ÍÀÌ´Ù. ÇöÀç business delegate ±¸ÇöÀÇ
ÇѰ踦 °ËÅäÇÑ ´ÙÀ½ ÈξÀ ÀϹÝÀûÀÌ¾î¼ ´ú °Á÷µÇ¾îÀÖ´Â business delegate Ŭ·¡½ºÀÇ ±¸ÇöÀ» ¸¸µé¾î¼
ÀÌ·¯ÇÑ ÇѰ踦 ±Øº¹ÇÏ´Â ¹æ¹ýÀ» º¸¿©ÁÖ°Ú´Ù.
business delegate class:
¸®ÇÁ·¹¼Å(refresher) Library ºó ÀÎÅÍÆäÀ̽º¿ë business delegate Ŭ·¡½º
ÂüÁ¶.
´ëºÎºÐÀÇ LibraryDelegate Ŭ·¡½º´Â ¿ø·¡ÀÇ
Library ºó¿¡ ÀÖ´Â ¸Þ¼ÒµåµéÀÌ ´ÜÁö Áߺ¹µÈ °ÍÀÌ´Ù.
LibraryDelegate ´Â init() ,
destroy() , ±¸Á¶Ã¼ ¸Þ¼Òµå¸¦ Ãß°¡ÇÏ°í ±×·±´ÙÀ½ ÀÌ·¯ÇÑ ¸Þ¼ÒµåµéÀ» »ç¿ëÇÏ¿©
Library ºóÀ¸·Î ŽºÅ©µéÀ» À§ÀÓÇÑ´Ù(delegate). ÀÌ·¸°Ô Çؼ
delegateÀº À¥ Ƽ¾î¿Í ¿£ÅÍÇÁ¶óÀÌÁî ºó »çÀÌÀÇ ¹öÆÛ ¿ªÇÒÀ» ÇÏ´Â °ÍÀÌ´Ù. ¿ø·¡ ºóÀÇ ºñÁö´Ï½º ÀÎÅÍÆäÀ̽º ÂüÁ¶.
¸Þ¼ÒµåÀÇ È®»ê ÀÌ·¯ÇÑ
Á¢±Ù¹æ½ÄÀÇ ¹®Á¦´Â ¸¹Àº ¼¼¼Ç ºóµéÀÌ 10°³, 20°³ ¶Ç´Â ±× ÀÌ»óÀÇ ¸Þ¼Òµå¸¦ °¡Áö°í ÀÖ´Ù´Â °ÍÀ» °í·ÁÇϱâ Àü±îÁö
¸íÈ®ÇØÁöÁö ¾Ê´Â´Ù. »ç½Ç 50°³ ÀÌ»óÀÇ ¸Þ¼Òµå¸¦ °¡Áø ¼¼¼ÇºóÀ» ¹ß°ßÇÏ´Â °ÍÀº ÈçÇÑÀÏÀÌ´Ù. ºóÀÇ ºñÁö´Ï½º
ÀÎÅÍÆäÀ̽º°¡ ºóÀÇ ¸ðµç ¸Þ¼Òµå¸¦ Æ÷ÇÔÇØ¾ß Çϱ⠶§¹®¿¡ business delegate Ŭ·¡½º°¡ ±×·² ¼ö ÀÖ´Ù.
ÀÌ°ÍÀº È®»êµÈ ÄÚµåÀÌ°í ¿¡·¯ÀÇ ¾ç»êÁöÀÌ´Ù.
ŸÀÌÇÎ ¼Óµµ°¡ ºü¸£´Ù±¸¿ä? EJB ÄÄÆ÷³ÍÆ® ÀÛ¾÷À»
ÇÒ ¶§, ¿ø°Ý ÀÎÅÍÆäÀ̽º, ºñÁö´Ï½º ÀÎÅÍÆäÀ̽º, ±¸Çö Ŭ·¡½º, business delegate µîÀ»
³Ñ³ªµé¸é¼ ¸¹Àº ¸Þ¼ÒµåµéÀÌ Áߺ¹µÈ´Ù. ¼ÕÀ¸·Î ÀÏÀÏÀÌ ¸Þ¼Òµå¸¦ ÀÔ·ÂÇÏ´Â °Í º¸´Ù, ¿¡µðÅÍ À©µµ¿ì¿Í
IDE »çÀÌ¿¡¼ "ÀÚ¸£±â(cut)" ¿Í "ºÙÀ̱â(paste)" ¶ó´Â ÁÁÀº ¹æ¹ýÀÌ ÀÖ´Ù. ÇÏÁö¸¸
Á¶½ÉÇضó. ¸Þ¼Òµå¸¦ ¼ÕÀ¸·Î ÀÔ·ÂÇÏ´Â °Í ¸¸ÅÀ̳ª Option+V ¶Ç´Â Control+V¸¦ ´©¸£´Â °Íµµ
½Ç¼ö°¡´É¼ºÀÌ ³ô´Ù. °Ô´Ù°¡ ¸Þ¼Òµå¸¦ Ãß°¡ÇÒ ¶§ ¿¡·¯ÀÇ ±âȸ°¡ µÈ´Ù. ¸Þ¼Òµå°¡ Á¤È®È÷ ÀԷµǰí ÀǵµÇÑ
´ë·Î "ÀÚ¸£±â(cut)" ¿Í "ºÙÀ̱â(paste)"°¡ Á¤È®È÷ µÇ¾ú´Ù´Â °ÍÀ» µÎ¹ø Á¤µµ È®ÀÎÇϸé
Àå¼öÇÏ°Ô µÉ °ÍÀÌ´Ù. :) |
È®»êµÇ´Â °Í ÀÌ¿Ü¿¡µµ º¯°æ ¿ä¼Ò¸¦ °í·ÁÇؾßÇÑ´Ù. Delegate Ŭ·¡½º´Â ¸ðµç ºóÀÇ
¸Þ¼ÒµåµéÀ» Áߺ¹½ÃÅ°°í ºóµéÀº ½Ã°£ÀÌ Áö³ª¸é ¾î¿ ¼ö ¾øÀÌ º¯°æµÇ±â ¶§¹®¿¡ »õ·Î¿î ¸Þ¼Òµå¸¦
Delegate ¿¡ Ãß°¡Çϴµ¥ ¸¹Àº ½Ã°£À» ÇÒ¾ÖÇÏ°Ô µÈ´Ù. »õ·Î¿î Äڵ带 ÀçÄÄÆÄÀÏÇÏ°í
Å×½ºÆ® ÇؾßÇÔÀº µÎ¸»ÇÒ ³ªÀ§ ¾ø´Ù.
ÀÌ ÀÚü´Â ±×´ÙÁö Å« ¹®Á¦°¡ ¾Æ´Ï´Ù. ÇÏÁö¸¸ business delegate Ŭ·¡½º¸¦ »ç¿ëÇÏ¿© ºñÁö´Ï½º¿Í
±¸Çö ·ÎÁ÷À» ±â¼ú ÀÎÇÁ¶ó¿¡¼ ÃßÃâÇÏ´Â °ÍÀ¸·Î ½ÃÀÛÇßÀ¸¸ç ÀÌ °æ¿ì EJB ÄÄÆ÷³ÍÆ®¶ó´Â °ÍÀ» °í·ÁÇغ¸¶ó. ¿ø°Ý
ÀÎÅÍÆäÀ̽º¸¦ º¯°æÀÌ business delegateÀ» º¯°æÀ» ¼ö¹ÝÇÑ´Ù¸é ¿ì¸®ÀÇ business delegateÀº
±â¹Ý ÄÄÆ÷³ÍÆ®¿¡ ¹ÀδÙ.
¿©±â ´õ ÁÁÀº ¹æ¹ýÀÌ ÀÖ´Ù.
µ¿Àû
delegate ÇØ°áÃ¥Àº dynamic delegateÀε¥ ÀÌ°ÍÀº Java
reflectionÀ» »ç¿ëÇÑ´Ù. delegateÀ¸·Î ÇϵåÄÚµùµÈ ºñÁö´Ï½º ¸Þ¼Òµå¸¦ °®´Â ´ë½Å ¸ñÇ¥ EJB
ÄÄÆ÷³ÍÆ®ÀÇ ¿ø°Ý ÀÎÅÍÆäÀ̽º»ó¿¡¼ delegateÀÌ ¸Þ¼Òµå¸¦ µ¿ÀûÀ¸·Î È£ÃâÇϵµ·Ï ÇÑ´Ù. ÀÌ°ÍÀº ¿ø°Ý ÀÎÅÍÆäÀ̽º¿¡¼
¿Ïº®ÇÑ µðÄ¿Çøµ(decoupling)ÀÌ °¡´ÉÇÏ´Ù. ºóÀÇ ºñÁö´Ï½º ¶Ç´Â ¿ø°Ý ÀÎÅÍÆäÀ̽º¿¡ ¸Þ¼Òµå¸¦ Ãß°¡ÇÑ´Ù°í Çؼ
ÀÌ¿¡ µû¸¥ business delegateÀ» º¯°æÇÒ ÇÊ¿ä°¡ ¾ø´Ù. µ¿Àû delegateÀ» ÀÌ¿ëÇÏ¸é ±â¼ú ÀÎÇÁ¶ó
º¯°æµµ ½¬¿öÁø´Ù. ¿ø°Ý ÀÎÅÍÆäÀ̽º¿Í EJB ±â¼ú¿¡¼ Java Data Objects (JDO)°°Àº ´Ù¸¥ ±â¼ú·Î
¸¶À̱׷¹ÀÌ¼Ç ÇÒ ¶§ ´ÜÁö delegateÀÇ init() ¸Þ¼Òµå¸¸ º¯°æÇÏ¸é µÈ´Ù. ´Ù¸¥
¸ðµç ¸Þ¼Òµå È£ÃâÀº ºóÀÇ ÀÎÅÍÆäÀ̽º¸¦ ÅëÇØ °è¼ÓÀûÀ¸·Î À§ÀӵȴÙ. º¯°æ¿¡ µû¸¥ Ãß°¡ÀÛ¾÷Àº ¾ø´Ù. Listing
1Àº Library business delegateÀÇ µ¿Àû ¹öÀüÀÌ´Ù: Listing 1. Library ºóÀ» À§ÇÑ business
delegate
package com.ibm.library;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.CreateException;
import javax.naming.NamingException;
public class LibraryDelegate implements ILibrary {
private ILibrary library;
private Map availableMethods;
public LibraryDelegate() {
init();
}
public void init() {
// Look up and obtain our session bean
try {
LibraryHome libraryHome =
(LibraryHome)EJBHomeFactory.getInstance().lookup(
"java:comp/env/ejb/LibraryHome", LibraryHome.class);
library = libraryHome.create();
// Get the methods available for use in proxying
availableMethods = new HashMap();
Method[] methods = ILibrary.class.getMethods();
for (int i=0; i<methods.length; i++) {
availableMethods.put(methods[i].getName(),
methods[i]);
}
} catch (NamingException e) {
throw new RuntimeException(e);
} catch (CreateException e) {
throw new RuntimeException(e);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
// All the hard-coded methods are removed
public Object
invoke(Object proxy, Method method, Object[] args)
throws Throwable{
try {
// See if this is init() or destroy()
if (method.getName().equals("init")) {
init();
return null;
} else if (method.getName().equals("destroy")) {
destroy();
return null;
} else {
Method method =
(Method)availableMethods.get(method.getName());
// See if we found anything
if (method != null) {
return method.invoke(library, args);
} else {
throw new
NoSuchMethodException("The Library does not " +
"support the " + method.getName() +" method.");
}
}
} catch (InvocationTargetException e) {
// We don't support throwing RuntimeExceptions from EJBs
// directly
if (e.getTargetException() instanceof RemoteException) {
throw new RuntimeException(e);
} else {
throw e.getTargetException();
}
}
}
public void destroy() {
// In this case, do nothing
}
}
|
µ¿Àû delegateÀº delegate, ºó, ºñÁö´Ï½º ÀÎÅÍÆäÀ̽º »çÀÌÀÇ Ä¿Çøµ ¹®Á¦¸¦ ÈǸ¢ÇÏ°Ô ÇØ°áÇÑ´Ù.
ÇÏÁö¸¸ ÀÌ°ÍÀº ¿Ïº®ÇÑ ¼Ö·ç¼Çµµ ÃÖ»óÀÇ ¼Ö·ç¼Çµµ ¾Æ´Ï´Ù. ÀÌ·¯ÇÑ Á¢±Ù¹æ½ÄÀº ¹«ÇÑÇÑ À¯¿¬¼ºÀ» Á¦°øÇØÁÖÁö¸¸ ÆÛÆ÷¸Õ½º¿¡
´ëÇؼ Èñ»ýÀ» ÇؾßÇÑ´Ù. ÀÚ¹Ù reflectionÀº ºü¸£Áö ¾Ê´Ù. µû¶ó¼
invoke() ¸¦ È£ÃâÇÏ°í °á°ú¸¦ ¾ò°ÔµÇ±â±îÁö »ó´çÇÑ ½Ã°£ÀÌ °É¸°´Ù. ÀÌÀü ÆÁ¿¡¼
¼³¸íÇß´ø Á¤Àû business delegateÀº Á» ´õ ºü¸¥ ¼Ö·ç¼ÇÀ̱â´Â ÇÏÁö¸¸ ºñÁö´Ï½º¿Í ±â¼ú ·¹À̾
Áö³ªÄ¡°Ô Ä¿ÇøµµÈ´Ù. µû¶ó¼ µÎ °¡Áö ¿É¼ÇÀ» °í·ÁÇÒ ¶§ µðÀÚÀÎÀ̳ª ÆÛÆ÷¸Õ½º¿¡ µû¸¥ ¼±ÅÃÀÌ µÇ¾î¾ßÇÑ´Ù. µ¿Àû
delegateÀº Àüü ÆÛÆ÷¸Õ½º º¸´Ù´Â ¾ÖÇø®ÄÉÀÌ¼Ç µðÀÚÀÎÀÌ ´õ Áß¿äÇÒ °æ¿ì ¼±ÅÃÇϸé ÁÁ´Ù. business
delegateÀº ÆÛÆ÷¸Õ½º°¡ ´õ¿í Áß¿äÇÑ ¿ä¼Ò°¡ µÉ ¶§ ÃÖ»óÀÇ ¼±ÅÃÀÌ µÈ´Ù.
Âü°íÀÚ·á
|