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

EJB best practices :delegationÀÇ ÇÑ°è
business delegateÀÇ ´ë¾È

Level: Intermediate

Brett McLaughlin
ÀÛ°¡/ÆíÁýÀÚ, O'Reilly and Associates
2002³â 12¿ù

Column iconbusiness delegate patternÀº EJB Ãß»óÈ­¿¡ À־ ´ëÁßÀûÀÎ ¼Ö·ç¼ÇÀÌÁö¸¸ ÇÑ°èµµ ÀÖ´Ù. Brett McLaughlinÀº ±× ÇѰ踦 À̾߱âÇÑ´Ù. ¶ÇÇÑ À̸¦ ´ëü ÇÒ ¹æ¾Èµµ °ËÅäµÉ °ÍÀÌ´Ù.

EJB best practices ½Ã¸®Áî¿¡¼­´Â business delegate pattern¿¡ ´ëÇØ ÀÚÁÖ ´Ù·ç¾ú´Ù. Áú¸®µµ·Ï.. :) ÀÌ ±ÛÀº business delegateÀÌ ÇÁ·Î±×·¡¹ÖÀÌ ¾²ÀÏ ¼ö ¾ø´Â »óȲ¿¡ ´ëÇÑ ±ÛÀÌ´Ù.

½ÇÁ¦ÀÇ delegate
Áö±Ý±îÁöÀÇ ¿¹Á¦ ¾ÖÇø®ÄÉÀ̼ǿ¡¼­, ºñÁö´Ï½º ·ÎÁ÷Àº ¼¼¼Ç ºó¿¡¼­ ¸íÈ®ÇÏ°Ô Á¤ÀǵǾú°í business delegateÀÇ ¸Þ¼Òµå´Â ¼¼¼Ç ºóÀÇ ¸Þ¼Òµå¸¦ ´Ü¼øÈ÷ ¸ð¹æÇß´Ù. ÀÌ·¯ÇÑ À¯ÇüÀÇ ½Ã³ª¸®¿À¿¡¼­´Â, ºóÀÇ ºñÁö´Ï½º ÀÎÅÍÆäÀ̽º¿Í business delegateÀº °­°áÇյȴÙ. ºñÁö´Ï½º ÀÎÅÍÆäÀ̽ºÀÇ ¸Þ¼ÒµåµéÀÌ º¯ÇÒ ¶§ business delegateÀÇ ¸Þ¼Òµåµé ¿ª½Ã º¯ÇØ¾ß ÇÑ´Ù. ÀÌ µðÀÚÀÎÀÌ ÀÌ·ÐÀûÀ¸·Î´Â ÀÛµ¿¿¡ ¹®Á¦°¡ ¾øÁö¸¸ ½ÇÁ¦ EJB ÇÁ·Î±×·¡¹Ö°ú´Â °Å¸®°¡ ¸Ö´Ù.

Á»´õ ÀϹÝÀûÀÎ ½Ã³ª¸®¿À´Â, ¼¼¼Ç ºóÀÌ ´ÜÀÏ À¯´ÖÀÇ ºñÁö´Ï½º ·ÎÁ÷À» ¼öÇàÇϵµ·Ï Á¶ÇÕµÉ ¼ö ÀÖ´Â »ç¿ë°¡´ÉÇÑ ¸Þ¼ÒµåµéÀÇ ¸ðÀ½À» Æ÷ÇÔÇÏ´Â °ÍÀÌ´Ù. °¢ ¼¼¼Ç ºó ¸Þ¼Òµå´Â ´õ Å« Æ®·£Àè¼ÇÀÇ ÀϺθ¦ Á¦°øÇÑ´Ù. Ŭ¶óÀ̾ðÆ®´Â ´ÜÀÏ ¿ÀÆÛ·¹À̼ÇÀ¸·Î º¸ÀδÙ. ÀÌ »óȲ¿¡¼­ business delegate patternÀ» ±¸ÇöÀº Á¶±Ý ºÎÁ·ÇÑ ¹æ¹ýÀÌ´Ù.

EJB ±â¼úÀÇ À̷аú ½ÇÁ¦
ÀÌ·ÐÀûÀ¸·Î EJB ¾ÖÇø®ÄÉÀ̼ÇÀº ÀüÀûÀ¸·Î ÄÄÆ÷³ÍÆ® ±¸µ¿ÀÌ°í ½ÇÁ¦·Î ÄÚµùÇϱâ Àü¿¡ ¼³°èµÈ´Ù. ¿£ÅÍƼºóÀº ¼¼¼Ç ÆÛÄÉÀ̵å(facade) ºóÀ» ÅëÇØ ¾ðÁ¦³ª ¾×¼¼½º µÇ¸ç ±×·¯ÇÑ ºóµéÀº Ãß°¡ ¼¼¼Ç ºóµé¿¡ ÀÇÇؼ­ Á»´õ ³í¸®ÀûÀÎ ºñÁö´Ï½º ´ÜÀ§·Î ±×·ìÇεȴÙ. ÀÌ ½Ã³ª¸®¿À¿¡ µû¸£¸é Ŭ¶óÀ̾ðÆ®¿¡°Ô ³ëÃâµÇ´Â À¯ÀÏÇÑ ¸Þ¼Òµå´Â purchaseBook()¿Í addUser() °°Àº ºñÁö´Ï½º ±¸µ¿ÀÇ ¸Þ¼ÒµåÀÌ´Ù.

ÇÏÁö¸¸ ½ÇÁ¦·Î ¿ì¸®°¡ º¼ ¼ö ÀÖ´Â °ÍÀº ½ºÆÄ°ÔƼ ¸ð¾çÀÇ ÄÚµùÀÏ °ÍÀÌ´Ù. ¿£ÅÍƼ ºóÀº óÀ½ºÎÅÍ ´Ù½Ã ¼³°èµÇ°í ±×·±´ÙÀ½ ¼¼¼Ç ºóµéÀÌ ¿£ÅÍƼ ºóµéÀÇ »ó´Ü¿¡ ³õÀδÙ. Á¾Á¾ ±× »çÀÌ¿¡ µðÀÚÀÎÀ̳ª Ç÷¡´× ½Ã°£ÀÌ ÀÖ±â´Â ÇÏ´Ù. ¿£ÅÍƼ ºóÀº ¾ðÁ¦³ª Ŭ¶óÀ̾ðÆ®¿¡ ³ëÃâµÇ´Â °ÍÀº ¾Æ´Ï¸ç, ºñÁö´Ï½º ·ÎÁ÷Àº ¿©·¯ °³ÀÇ ¸Þ¼Òµå°¡ ¼¼¼Çºó(¿¹¸¦ µé¾î, getBookISBN(), reserveBook(), decreaseInventory(), makePurchase() ¼øÀ¸·Î)¿¡ È£Ãâ µÉ °ÍÀ» ¿äûÇÑ´Ù. ÇϳªÀÇ ºñÁö´Ï½º ½ºÆÑÀÇ ¸Þ¼Òµå¸¦ È£ÃâÇÏ´Â ´ë½Å Ŭ¶óÀ̾ðÆ®´Â ¸ðµç ¼­¹ö ¾ÖÇø®ÄÉÀ̼ÇÀÇ ºñÁö´Ï½º ·ÎÁ÷À» ¸ðµÎ °íÃľßÇÑ´Ù.

business delegateÀÌ ÀÛµ¿ÇÏÁö ¾ÊÀ» ¶§
ÀÌ °æ¿ì business delegate°ú °ü·ÃÇÑ ¹®Á¦´Â, ÀÌ°ÍÀÌ À§ÀÓÀº(delegate)ÇÏÁö¸¸ ½ÇÁ¦·Î´Â ºñÁö´Ï½º¸¦ ¼öÇàÇÏÁö´Â ¾Ê´Â´Ù´Â Á¡ÀÌ´Ù. delegateÀº Ŭ¶óÀ̾ðÆ®°¡ ¼¼¼Ç ºóÀÇ ¸Þ¼Òµå¿¡ ¾×¼¼½ºÇϵµ·Ï ÇÏÁö¸¸ Ŭ¶óÀ̾ðÆ®´Â ¿©ÀüÈ÷ ±×·¯ÇÑ ¸Þ¼Òµå¸¦ ºñÁö´Ï½º À¯´ÖÀ¸·Î ¾î¼Àºí¸µÇؾßÇÑ´Ù. ´Ù¾çÇÑ ÇÁ·Î±×·¡¸ÓµéÀÌ ¿©·¯°¡Áö ¹æ½ÄÀ¸·Î ¸Þ¼Òµå¸¦ ¾î¼ÀºíÇÏ°í Á¤·ÄÇϱ⠶§¹®¿¡ Ŭ¶óÀ̾ðÆ®´Â ´Ù¾çÇÑ ¹æ½ÄÀ¸·Î ºñÁö´Ï½º ÇÁ·Î¼¼½º¸¦ ½ÇÇàÇÒ °ÍÀÌ´Ù. ¾î¶² Ŭ¶óÀ̾ðÆ®´Â Áß¿äÇÑ ´Ü°è¸¦ »ý·«Çϱ⵵ ÇÏ°í ¾î¶² Ŭ¶óÀ̾ðÆ®´Â ÇÁ·Î¼¼½º ½Ã°£À» ´Ã¸®´Â »õ·Î¿î ´Ü°è¸¦ Ãß°¡ÇÒ ¼öµµ ÀÖ´Ù. ´ÙÁß Å¬¶óÀ̾ðÆ®¸¦ ÅëÇØ ÀÏ°ü¼ºÀÖ´Â ¾ÖÇø®ÄÉÀÌ¼Ç ÆÛÆ÷¸Õ½º¸¦ º¸ÀåÇÏ·Á¸é ´ç½ÅÀÇ ¾ÖÇø®ÄÉÀ̼ÇÀÇ ºñÁö´Ï½º ·ÎÁ÷À» ÀÎÅÍÇÁ¸®ÆÃÇÏ´Â ÁüÀ» ´ú¾îÁà¾ßÇÑ´Ù.

¿ì¸®¿¡°Ô ÇÊ¿äÇÑ °ÍÀº business delegateó·³ º¸À̸鼭 ÀÛµ¿ÇÏÁö¸¸ ½ÇÁ¦·Î´Â Ŭ¶óÀ̾ðÆ®¿¡°Ô ºñÁö´Ï½º °üÁ¡ÀÇ ¾ÖÇø®ÄÉÀ̼ÇÀ» Á¦°øÇϴ Ŭ·¡½ºÀÌ´Ù. ¿©±â¿¡¼­´Â ±×·¯ÇÑ À¯ÇüÀÇ Å¬·¡½º¸¦ command delegate À̶ó°í ÇÏ°Ú´Ù. ¼¼¼Ç ºóÀ¸·Î Á÷Á¢ ¾×¼¼½ºÇÏ´Â ´ë½Å¿¡ command delegateÀº ¿©·¯ ¼¼¼Ç ºó ¸Þ¼Òµå·Î À§ÀӵǴ ¸í·É¾î(command)¸¦ Á¦°øÇÑ´Ù.

command delegate
command delegateÀÌ business delegate º¸´Ù ³ªÀº ÇÁ·Î±×·¡¹Ö ¼Ö·ç¼ÇÀ̶ó´Â °ÍÀ» ÀÔÁõÇÏ´Â ½Ã³ª¸®¿À¸¦ °ËÅäÇغ¸ÀÚ. Listing 1Àº Inventory ºó¿ë ºñÁö´Ï½º ÀÎÅÍÆäÀ̽ºÀÌ´Ù. ¿Â¶óÀÎ ¿Àµð¿À-ºñµð¿À ¸ÅÀå Àç°í(Inventory)¸¦ °ü¸®ÇÑ´Ù :

Listing 1. Inventory ºóÀÇ ºñÁö´Ï½º ÀÎÅÍÆäÀ̽º

package com.ibm.inventory;

import java.rmi.RemoteException;

public interface IInventory {

      public String getItemId(String productName)
	    throws RemoteException;

      public boolean reduceInventory(String itemId, int quantity)
	    throws RemoteException;

      public boolean increaseInventory(String itemId, int quantity)
	    throws RemoteException;

      // Overloaded version: reduce inventory by one
      public boolean reduceInventory(String itemId)
	    throws RemoteException;

	// Overloaded version: increase inventory by one
	public boolean increaseInventory(String itemId)
		throws RemoteException;
}

Listing 2´Â ±¸¸Å (purchasing) ºó¿¡ ´ëÇÑ ºñÁö´Ï½º ÀÎÅÍÆäÀ̽ºÀÌ´Ù:

Listing 2. ±¸¸ÅÀÚ(Purchaser) ºóÀÇ ºñÁö´Ï½º ÀÎÅÍÆäÀ̽º

package com.ibm.purchaser;

import java.rmi.RemoteException;

public interface IPurchaser {

      public boolean makePayment(PurchaseInfo purchaseInfo, float amount)
	    throws RemoteException;

      public boolean makeCredit(PurchaseInfo purchaseInfo, float amount)
	    throws RemoteException;
}

À§ ±¸¸Å ½Ã³ª¸®¿À´Â ¿©·¯ ´Ü°è·Î ±¸¼ºµÇ¾îÀÖ´Ù. ¿ì¼±, ±¸¸Å ¾ÆÀÌÅÛÀÇ ¾ÆÀ̵𰡠°Ë»öµÈ´Ù. ±×·±´ÙÀ½ °áÁ¦°¡ ÀÌ·ç¾îÁø´Ù. ¸¶Áö¸·À¸·Î, °áÁ¦°¡ ¿Ï·áµÇ¸é ±¸¸Å ¾ÆÀÌÅÛ¿¡ ´ëÇÑ Àç°í°¡ °¨¼ÒÇϰԵȴÙ. business delegate patternÀ» »ç¿ëÇÏ¿© ÀÌ ½Ã³ª¸®¿À¸¦ ÇÚµéÇÑ´Ù¸é µÎ °¡Áö business delegate Ŭ·¡½º°¡ ÇÊ¿äÇÏ°í Àû¾îµµ ¼¼ °³ÀÇ ¸Þ¼Òµå È£ÃâÀÌ ÇÊ¿äÇÏ´Ù.

command delegate ÆÐÅÏÀ» ¼±ÅÃÇÏ´Â °Íµµ ÁÁÀº ¹æ¹ýÀÌ´Ù. business delegateÀÌ Å¬¶óÀ̾ðÆ®¿¡°Ô ºñÁö´Ï½º ·ÎÁ÷ÀÇ °³º° ûũ·ÎÀÇ ¾×¼¼½º¸¦ Çã¿ëÇÏ´Â ¹Ý¸é command delegate Ŭ·¡½º´Â ¼¼¼Ç ºóÀ» ÅëÇؼ­ ÀÛµ¿ÇÏ°í ºñÁö´Ï½º ·ÎÁ÷À» Çѵ¥ ¸ðÀ¸¸ç À̸¦ Ŭ¶óÀ̾ðÆ®¿¡°Ô ½ÇÇà ¸Þ¼Òµå·Î¼­ Á¦°øÇÑ´Ù. (Listing 3):

Listing 3. PurchaserDelegate Ŭ·¡½º

package com.ibm.purchase;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.naming.NamingException;

import com.ibm.purchaser.*;
import com.ibm.inventory.*;

public class PurchaserDelegate {

      private IPurchaser purchaser;
	private IInventory inventory;

      public PurchaserDelegate() {
          init();
      }

      public void init() {
          // Look up and obtain our session beans
          try {
            PurchaserHome purchaserHome =
              (PurchaserHome)EJBHomeFactory.getInstance().lookup(
                "java:comp/env/ejb/PurchaserHome", PurchaserHome.class);
            purchaser = purchaserHome.create();

            InventoryHome inventoryHome =
              (InventoryHome)EJBHomeFactory.getInstance().lookup(
                "java:comp/env/ejb/InventoryHome", InventoryHome.class);
            inventory = inventoryHome.create();
          } catch (NamingException e) {
              throw new RuntimeException(e);
          } catch (CreateException e) {
              throw new RuntimeException(e);
          } catch (RemoteException e) {
              throw new RuntimeException(e);
          }
      }

	public boolean purchase(String itemName, float price, 
	PurchaseInfo purchaseInfo)
	    throws InsufficientFundsException, PurchaserException {

		try {
		    String itemID = inventory.getItemId(itemName);
			if (purchaser.makePurchase(purchaseInfo, price)) {
			    inventory.reduceInventory(itemID);
				return true;
			} else {
			    throw new InsufficientFundsException();
			}
		} catch (RemoteException e) {
		    throw new PurchaserException(e);
	    }
      }

      // Other business-specific methods

      public void destroy() {
          // In this case, do nothing
      }
}

Á»´õ Çö½ÇÀûÀÎ ¿¹Á¦¿¡¼­´Â ¹°·Ð ºñÁö´Ï½º ·ÎÁ÷Àº ´õ¿í º¹ÀâÇÏ´Ù. ¿¹¸¦µé¾î Ŭ¶óÀ̾ðÆ®°¡ ±¸¸Å ¾ÆÀÌÅÛÀ» ¼±ÀûÇÏ´Â ¹®Á¦¸¦ ´Ù·ï¾ß ÇÑ´Ù¸é ¼îÇÎ Ä«Æ®´Â ÇÑ ¹ø¿¡ ÇÑ °³ ÀÌ»óÀÇ ¾ÆÀÌÅÛÀ» °®°íÀÖ¾î¾ß ÇÑ´Ù. ¶Ç´Â ¾ÆÀÌÅÛ µð½ºÄ«¿îÆ®, ÀÚµ¿ Àç°í Á¶»ç, ÁÖ¹® ÇÁ·Î¼¼½ºµîÀ» ó¸®ÇÏ´Â ±â´ÉÀÌ ÀÖ¾î¾ß ÇÑ´Ù. µÎ °³ÀÇ ºóÀÌ ¾Æ´Ñ ¼¼°³ ¶Ç´Â ³×°³ÀÇ ºó°ú ´õ¿í ¸¹Àº ¸Þ¼Òµå¿Í ÀÎÅÍ·¢Æõµ ÇÊ¿äÇÏ´Ù. ÇÏÁö¸¸ ¾ÖÇø®ÄÉÀ̼ÇÀÇ º¹À⼺°ú °ü°è¾øÀÌ command delegate patternÀº ÇϳªÀÇ ¸Þ¼Òµå È£Ãâ·Î À̵éÀ» ó¸®ÇÒ ¼ö ÀÖ´Ù. command delegate patternÀ» ÀÌ¿ëÇÏ¸é ºñÁö´Ï½º¿Í EJB Ƽ¾îÀÇ º¹ÀâÇÔÀÌ ¾ÖÇø®ÄÉÀÌ¼Ç Å¬¶óÀ̾ðÆ®·Î ¿Å°Ü°¡Áö ¾Ê´Â´Ù. ±×°ÍÀº ºÐ¸í ÁÁÀº ÇÁ·Î±×·¡¹Ö ±â¼úÀÌ ¾Æ´Ò ¼ö ¾ø´Ù.

´Ù¾çÇÑ EJB µðÀÚÀÎ ÆÐÅÏÀ» ÅëÇØ ¼ÒÆÃÇÏ°í ¾î¶² °ÍÀÌ ½ÇÁ¦ ½Ã³ª¸®¿À¿¡ ÀûÇÔÇÑÁö¸¦ ±¸º°ÇÏ´Â °ÍÀº ´Ù¼Ò È¥¶õ½º·´´Ù. »õ·Î¿î ÆÐÅÏÀ» Åø¹Ú½º¿¡ Ãß°¡ÇÏ°í ´Ù¾çÇÑ »óȲ¿¡ ¸ÂÃç ½ÃµµÇÒ ¶§ ¾Ë¸ÂÀº ¼Ö·ç¼ÇÀ» ã¾Æ°¡°Ô µÉ °ÍÀÌ´Ù. command delegate patternÀº º¹ÀâÇÑ ºñÁö´Ï½º ·ÎÁ÷À» Àû¿ëÇÏ´Â ¾ÖÇø®ÄÉÀ̼ǿ¡ ÀûÇÕÇÏ´Ù.

Âü°íÀÚ·á

¸ñ Â÷:
½ÇÁ¦ÀÇ delegate
business delegateÀÌ ÀÛµ¿ÇÏÁö ¾ÊÀ» ¶§
command delegate
Âü°í ÀÚ·á
ÇÊÀÚ ¼Ò°³
±â»ç¿¡ ´ëÇÑ Æò°¡
°ü·Ã dW ¸µÅ©:
EJB best practices series
Enterprise JavaBeans fundamentals
Best practices in EJB exception handling
Subscribe to the developerWorks newsletter
US ¿ø¹® Àбâ
Also in the Java zone:
Tutorials
Tools and products
Code and components
Articles
ÇÊÀÚ¼Ò°³
Photo of Brett McLaughlin Brett McLaughlinÀº ÇöÀç ÀÚ¹Ù ¹× °ü·Ã ±â¼úÀ» ÀÌ¿ëÇÏ¿© ¾ÖÇø®ÄÉÀÌ¼Ç ±â¹Ý±¸Á¶ ±¸ÇöÀ» Àü¹®ÀûÀ¸·Î ¼öÇàÇÏ°í ÀÖ´Ù. BrettÀº Java Apache ÇÁ·ÎÁ§Æ®ÀÎ TurbineÀÇ °øµ¿ ⸳ÀÚÀÌ´Ù.
ÀÌ ±â»ç¿¡ ´ëÇÏ¿© ¾î¶»°Ô »ý°¢ÇϽʴϱî?

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

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