Üzleti delegált programtervezési minta
Az üzleti delegált programtervezési minta csökkenti a redundanciát, azaz az üzleti és a prezentációs rétegben is jelen levő adatok mennyiségét, továbbá elrejti a szolgáltatások implementációját, beleértve a névszolgáltatásokat is.[1][2] Az adapter szerepét töltik be az üzleti objektumok és a prezentációs réteg között.[3] Használatára példa a Java EE.[1]
Szerkezete
A kliensek kérését az üzleti delegáltak a névszolgáltatás használatával közvetítik a kiszolgáló objektumoknak.[1] Komponensei az üzleti delegált, a névtérszolgáltató és az üzleti szolgáltatás.[1] Az üzleti delegált ellenőrzi és védi a többi összetevőt. A Java EE-ben kétféle lehet, ID-vel és ID nélküli, ahol az ID string verziója egy távoli objektumra mutató referenciának, mint az EJBHome vagy EJBObject.[1] A névtérszolgáltató lehetővé teszi az üzleti szolgáltatás elérését. Ezt az üzleti delegált használja, és az üzleti névtérszolgáltatót az általános névtérszolgáltató tartalmazza.[1] Az üzleti szolgáltatás az üzleti réteg komponense, mint enterprise bean vagy JMS komponens, ami a szolgáltatást nyújtja.[1]
Előnyei
- Csökken a redundancia
- Nő a rugalmasság, javul a karbantarthatóság
- Egységes API a prezentációs rétegnek az üzleti logikához való hozzáféréshez.[1][3]
Lehetséges problémák
- Extra réteg az alkalmazásban, ami bonyolultabb szerkezetet és több osztályt igényel. Az alkalmazás mérete és bonyolultsága is nő.
- Az üzleti delegáltnak követnie kell a távoli objektumok interfészeinek változását, emiatt ezen csak ritkán szabad változtatni.[3]
Példa
Az alábbi példa egy Professional Services Application (PSA), alkalmazásból való. A web rétegben a kliensnek egy session beanhez kell hozzáférnie, ami a homlokzat mintát valósítja meg. Erőforrás delegált:[1]
public class ResourceDelegate {
// Remote reference for Session Facade
private ResourceSession session;
// Class for Session Facade's Home object
private static final Class homeClazz =
corepatterns.apps.psa.ejb.ResourceSessionHome.class;
// Default Constructor. Looks up home and connects
// to session by creating a new one
public ResourceDelegate() throws ResourceException {
try {
ResourceSessionHome home = (ResourceSessionHome)
ServiceLocator.getInstance().getHome(
"Resource", homeClazz);
session = home.create();
} catch(ServiceLocatorException ex) {
// Translate Service Locator exception into
// application exception
throw new ResourceException(...);
} catch(CreateException ex) {
// Translate the Session create exception into
// application exception
throw new ResourceException(...);
} catch(RemoteException ex) {
// Translate the Remote exception into
// application exception
throw new ResourceException(...);
}
}
public BusinessDelegate(String id)
throws ResourceException {
super();
reconnect(id);
}
public String getID() {
try {
return ServiceLocator.getId(session);
} catch (Exception e) {
// Throw an application exception
}
}
public void reconnect(String id)
throws ResourceException {
try {
session = (ResourceSession)
ServiceLocator.getService(id);
} catch (RemoteException ex) {
// Translate the Remote exception into
// application exception
throw new ResourceException(...);
}
}
public ResourceTO setCurrentResource(
String resourceId)
throws ResourceException {
try {
return session.setCurrentResource(resourceId);
} catch (RemoteException ex) {
// Translate the service exception into
// application exception
throw new ResourceException(...);
}
}
public ResourceTO getResourceDetails()
throws ResourceException {
try {
return session.getResourceDetails();
} catch(RemoteException ex) {
// Translate the service exception into
// application exception
throw new ResourceException(...);
}
}
public void setResourceDetails(ResourceTO vo)
throws ResourceException {
try {
session.setResourceDetails(vo);
} catch(RemoteException ex) {
throw new ResourceException(...);
}
}
public void addNewResource(ResourceTO vo)
throws ResourceException {
try {
session.addResource(vo);
} catch(RemoteException ex) {
throw new ResourceException(...);
}
}
// all other proxy method to session bean
...
}
A ResourceSession távoli interfésze:[1]
public class ResourceDelegate {
// Remote reference for Session Facade
private ResourceSession session;
// Class for Session Facade's Home object
private static final Class homeClazz =
corepatterns.apps.psa.ejb.ResourceSessionHome.class;
// Default Constructor. Looks up home and connects
// to session by creating a new one
public ResourceDelegate() throws ResourceException {
try {
ResourceSessionHome home = (ResourceSessionHome)
ServiceLocator.getInstance().getHome(
"Resource", homeClazz);
session = home.create();
} catch(ServiceLocatorException ex) {
// Translate Service Locator exception into
// application exception
throw new ResourceException(...);
} catch(CreateException ex) {
// Translate the Session create exception into
// application exception
throw new ResourceException(...);
} catch(RemoteException ex) {
// Translate the Remote exception into
// application exception
throw new ResourceException(...);
}
}
public BusinessDelegate(String id)
throws ResourceException {
super();
reconnect(id);
}
public String getID() {
try {
return ServiceLocator.getId(session);
} catch (Exception e) {
// Throw an application exception
}
}
public void reconnect(String id)
throws ResourceException {
try {
session = (ResourceSession)
ServiceLocator.getService(id);
} catch (RemoteException ex) {
// Translate the Remote exception into
// application exception
throw new ResourceException(...);
}
}
public ResourceTO setCurrentResource(
String resourceId)
throws ResourceException {
try {
return session.setCurrentResource(resourceId);
} catch (RemoteException ex) {
// Translate the service exception into
// application exception
throw new ResourceException(...);
}
}
public ResourceTO getResourceDetails()
throws ResourceException {
try {
return session.getResourceDetails();
} catch(RemoteException ex) {
// Translate the service exception into
// application exception
throw new ResourceException(...);
}
}
public void setResourceDetails(ResourceTO vo)
throws ResourceException {
try {
session.setResourceDetails(vo);
} catch(RemoteException ex) {
throw new ResourceException(...);
}
}
public void addNewResource(ResourceTO vo)
throws ResourceException {
try {
session.addResource(vo);
} catch(RemoteException ex) {
throw new ResourceException(...);
}
}
// all other proxy method to session bean
...
}
Jegyzetek
- ↑ 1,00 1,01 1,02 1,03 1,04 1,05 1,06 1,07 1,08 1,09 Core J2EE Patterns - Business Delegate. Oracle . Oracle. (Hozzáférés: 2016. június 22.)
- ↑ Screening Technical Design Document - Version 2.0. Indiana, USA: Indiana state, 7. o.
- ↑ 3,0 3,1 3,2 Kayal, D.. Pro Java EE Spring Patterns. New York: Apress, 161–166. o. (2008)
Fordítás
- Ez a szócikk részben vagy egészben a Business delegate pattern című angol Wikipédia-szócikk fordításán alapul. Az eredeti cikk szerkesztőit annak laptörténete sorolja fel. Ez a jelzés csupán a megfogalmazás eredetét és a szerzői jogokat jelzi, nem szolgál a cikkben szereplő információk forrásmegjelöléseként.