Take the 2-minute tour ×

I understand that a delegate is used to decouple client and business layer. A client invokes a business service through delegate object. My question is that is it good to have interface of delegate exposed to client (Design by contract principle OR DIP) not the actual delegate object. Or is it just fine to have a delegate object accessed by the client directly.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

I would opt for the interface, because that would allow me to separate what's done from the how. I might find it advantageous to made the delegate a proxy or decorate it with an aspect; both are easy with an interface.

share|improve this answer
Is it possible to explain (made the delegate a proxy or decorate) using some example? –  Amit Apr 3 '11 at 19:46
see Dynamic Proxies in Java. Allows you to forward invocations over an interface to a chosen implementation, without much coding. –  Vladimir Dyuzhev Apr 3 '11 at 20:13
add comment

I believe you may be confusing the external API (facade) that client uses and the code that actually get invoked via delegate:

public class Worker implements IWorker {
    private IProcessor delegate;

    void doWork(PublicDO data) {
      PrivateDO pdo = convert(data);

Delegate not necessary has the same interface as facade. Moreover, delegate interface can be changed while the public interface is kept unchanged. There are may be multiple delegate implementation that differ slightly, instantiated at runtime based on some parameters.

Thus it is always a good idea to separate public interface and delegate interface.

Delegate having the same interface as public facade is just the simplest case.

share|improve this answer
Is it right to say that Session Facade should only b used when there is EJB involved at business layer. I am going to use spring framework as core for my app. I think I dont need facade right? So my flow should be Client -> Delegate (interface) -> delegate (impl) -> Business Obj (interface) -> (BO POJO) -> database layer? –  Amit Apr 4 '11 at 5:37
You're reading too many books. –  Vladimir Dyuzhev Apr 4 '11 at 20:49
Actually I read online articles and finds too many thing about a topic and gets confused. Is there any thing wrong / complex in my approach. Please correct. –  Amit Apr 5 '11 at 8:59
add comment

You can see wikipedia:


there's snippet in java


share|improve this answer
In both the example the client is directly using the delegate implementation object. Does that mean the interface for delegate is not required? –  Amit Apr 3 '11 at 19:45
@Amit: It's not required, but it's good practice to use the interface if you ever want to change the delegate to another implementation. –  Jorn Apr 3 '11 at 20:50
add comment

Your Answer


