The business delegate pattern puts a single intermediary between presentation code and business services, so clients call one local object instead of hunting down remote lookups, retries, and exception translation themselves. In Java, you implement it by defining a BusinessService interface, writing one or more concrete service implementations, wrapping them in a BusinessDelegate that handles lookup and failure policy, and giving clients a BusinessLookup or factory to resolve the delegate. The pattern earns its keep when service access is remote, fragile, or expensive to change; for a simple in-process POJO call, the extra layer usually costs more than it saves.
The sections below build a working example, then stress-test it against real conditions: lookup caching, checked exceptions that leak through proxies, thread safety, and the circular dependency trap that catches most first implementations.
What the pattern actually fixes, and what it does not
The classical motivation for the business delegate is a presentation tier that talks to an EJB or a remote service. Without the delegate, every controller repeats the same JNDI lookup, the same try/catch for NamingException and RemoteException, and the same retry logic. When the remote interface changes, the change ripples through every caller.
The delegate collapses that into one class. Clients see a coarse-grained method like placeOrder(...) and know nothing about naming services, serialization, or network policy. The lookup object handles the "where is the service" question; the delegate handles the "how do I call it safely" question.
What it does not fix is worth stating plainly. It is not a substitute for a proper service layer, it does not make a slow remote call fast, and it does not by itself give you transactions or security. It is a seam — a place to put the cross-cutting concerns that would otherwise be scattered. If your code calls a local Spring bean, the delegate is usually ceremony; if it crosses a process boundary, a flaky network, or a legacy naming service, the seam pays for itself quickly.
Prerequisites before you write any code
You need four things settled before the first line is useful.
First, decide the service boundary. The delegate should expose one task-oriented API, not a mirror of the remote interface. If the remote service has fifteen methods and clients need four, expose four.
Second, know where the service comes from. A javax.naming.InitialContext lookup for EJB, a JNDI-bound DataSource, a factory, or an injected HTTP client all produce different failure modes. The lookup object is where that knowledge lives, so define it precisely.
Third, decide your exception policy. Will the delegate translate checked exceptions into an unchecked BusinessDelegateException, or wrap them in a domain exception? This decision affects every client, so make it deliberately.
Fourth, pick your threading model. Is the delegate stateless and shared, or per-request? Most production delegates are stateless and shared, which means no mutable fields outside a thread-safe cache.
You'll also want a build with the JDK you actually target. The example uses java.util.concurrent.ConcurrentHashMap and java.util.function types, so Java 8 or later is fine; nothing here needs a newer release. If you use a naming service, add the appropriate client library and a test container or an in-memory stub so you can exercise failure paths without a live server.
Step 1: Define a coarse-grained service interface
Start with the contract clients will see. Keep it narrow and task-oriented.
OrderServiceException is your own checked exception, declared here rather than leaking RemoteException or NamingException. This is the first half of the isolation the pattern promises: the remote failure vocabulary stops at the interface boundary.
Expect this step to take longer than it looks. The interface is the stable thing; implementations churn. If you find yourself adding a method that "just passes through" to an internal call, that is a sign the boundary is drawn too low.
Step 2: Write the concrete service implementations
The concrete service hides the transport. A JNDI-backed implementation might look like this:
The important structural point: this class is the only place that knows about JDBC, JNDI, or whatever transport you use. You can swap it for a REST client, a mock, or an in-memory map without touching the delegate or the client. For tests, an InMemoryOrderService backed by a HashMap gives you fast, deterministic behavior; you'll use it heavily in the next steps.
Step 3: Build the BusinessLookup
The lookup object resolves a service by name and hides the naming mechanism.
Two design choices matter here. computeIfAbsent gives you a thread-safe cache with a single resolution per key under contention, which is what you want because JNDI lookups are slow and occasionally flaky. Wrapping NamingException in your own LookupException keeps the naming vocabulary out of the delegate.
The failure you should expect: InitialContext construction itself can throw NamingException if the environment is misconfigured, and the cached value will never be produced, so every call retries the lookup. That is usually the right behavior for a transient failure, but if the service is permanently gone you'll hammer the naming server. A simple guard is a failure timestamp and a short cooldown, or computeIfAbsent replaced by an explicit get/put that records negative results for a bounded window.
Step 4: Write the BusinessDelegate
The delegate owns the calling policy: resolve, invoke, translate failures, maybe retry.
A few things to notice. Retries are bounded and gated by a classification function — retrying a constraint violation is pointless and can duplicate side effects. The delegate throws an unchecked BusinessDelegateException because recoverable-at-the-call-site is rare for these failures; if your callers genuinely handle them, keep it checked. And the delegate holds no per-request state, so a single instance can be shared.
That retry loop deserves a warning. Retrying a non-idempotent write can double-charge a customer. Either make the service accept an idempotency key, or restrict retries to reads and idempotent updates. The pattern doesn't decide this for you; the classification function does.
Step 5: Wire the client
The client is now trivial:
The client imports nothing from the transport layer. That's the test of whether you implemented the pattern at all — grep the client for NamingException, DataSource, or RemoteException; none should appear.
Step 6: Verify behavior with tests, including failures
Success path first: with an InMemoryOrderService behind the lookup, placeOrder returns ACCEPTED, and a second call for the same id returns the stored record. That's your happy-path check.
Failure paths are where the pattern earns its money, so test them explicitly. A service that throws OrderServiceException wrapping a SQLTransientException should produce exactly maxAttempts invocations of the underlying service. A service throwing a constraint-violation-style exception should produce exactly one. A lookup that throws LookupException should surface as BusinessDelegateException without any invocation.
You can assert invocation counts with a hand-rolled counting stub if you don't want a mocking library:
The success criterion is a green suite where the retry test fails the moment you change maxAttempts to 1. If it still passes, your test isn't reaching the retry branch.
Where implementations break: five failure modes
The first trap is the circular dependency. If the concrete service implementation calls back into a client that holds the delegate, you get a loop that can deadlock under a lock or spin until timeout. Lookup caching makes this worse because the cached instance is shared. Keep services downstream-only.
The second is stale negative caching. If your lookup remembers a failed resolution, a recovered service stays invisible until the cooldown expires. Bound the TTL and log resolutions so you can see the delay.
The third is exception erasure. Wrapping everything in RuntimeException loses the distinction between "retry" and "give up," so your retry loop becomes a duplicate-order generator. Always preserve the cause chain.
The fourth is thread-unsafe delegate state. A delegate that caches a Context field or accumulates counters without atomics will misbehave under load. Keep delegates stateless, or make the state explicitly thread-safe.
The fifth is interface creep. When the delegate grows a method per remote call, it becomes a fat proxy and the seam you built for isolation turns into a second interface to maintain. Prune aggressively.
Business delegate versus facade, proxy, and service locator
These four patterns get confused because they all sit between a caller and something else.
The facade is a simplified interface over a subsystem. It doesn't necessarily isolate transport or handle lookup; it just makes calls easier. A business delegate often uses a facade internally.
The proxy implements the same interface as the real subject and intercepts calls, typically for lazy loading, access control, or remote invocation. A dynamic proxy is a mechanism you might use to implement a delegate's invocation logic, but the delegate usually changes the interface shape, which a classic proxy does not.
The service locator answers "where is the service" and nothing else. It's essentially the BusinessLookup in isolation. Some descriptions treat the service locator as the delegate's lookup component; that's accurate, but it's important not to call the locator a delegate, because nothing there owns the calling policy.
| Criterion | Business Delegate | Facade | Proxy | Service Locator |
|---|---|---|---|---|
| Primary job | Isolate client from service access | Simplify a subsystem | Intercept calls to one subject | Resolve services by name |
| Changes interface shape | Often, to task methods | Yes, simplified | No, same interface | No |
| Handles failures/retries | Yes, typically | Rarely | Sometimes | No |
| Knows transport details | Yes | Sometimes | Usually | Yes |
Use the table as a decision aid, not a taxonomy exercise. The real question is which object should own retry, translation, and lookup. Put them in one place and name that place a delegate.
When to choose it, and when it costs too much
Choose the business delegate when at least two of these hold: access crosses a process or naming boundary, failure handling is repeated across callers, the service interface is unstable or foreign, or you need a single place to add caching or circuit-breaking later.
Skip it when the service is a local bean injected by your framework. In Spring, an @Service injected into a controller already gives you a seam; adding a delegate in front is a layer with no distinct job, and it will drift. Skip it too for a single caller — the isolation pays for itself across many callers or over time, not on day one.
The cost is real: one extra indirection, one more class that can hide a bug, and a testing obligation for failure paths that most teams underestimate. If you can't name the failure policy the delegate enforces, you're adding ceremony without benefit.
A production hardening checklist
Before shipping, run through these and be able to answer each.
Is the delegate stateless, and if not, is every mutable field atomic or immutable? Does the lookup cache have a bounded lifetime, and is negative caching bounded? Are retries restricted to idempotent operations or paired with an idempotency key? Is the cause chain preserved through every wrapper? Are timeouts configured on the underlying client, since a delegate with retries and no timeout multiplies the wait? Does the client import anything from the transport package? Is there a test that asserts the exact invocation count on failure?
If a remote or naming dependency remains flaky after you've bounded retries, added timeouts, and capped the cache, that's a specific stop condition: the failure is in the service or network, not the delegate, and further work belongs with whoever operates that service. Keeping the delegate as a clean seam is still valuable, but it won't fix an upstream problem.
FAQ
Can I use a dynamic proxy instead of writing the delegate by hand? Yes, for the invocation part: java.lang.reflect.Proxy can intercept every method of a service interface and apply a shared retry-and-translate policy. You'll still want a hand-written lookup class, because proxies don't resolve services. For small interfaces, a hand-written delegate is easier to read and debug.
Should the delegate be a Spring bean? If you're in a Spring application, yes — a singleton @Component wired with the lookup and configuration keeps it stateless and testable. The pattern is framework-agnostic, and the shape is the same whether you wire it by hand or by container.
Does it replace the DAO pattern? No. A DAO encapsulates database access; a business delegate encapsulates service access. They often coexist, with the delegate calling a service that in turn uses DAOs. Conflating them produces a class that does two jobs badly.
What about async or reactive clients? The pattern still applies: the service interface returns CompletableFuture or a reactive type, and the delegate owns the retry and error-mapping policy for that type. The mechanics shift, the seam doesn't.