79

What are the differences between proxy, wrapper or a façade classes

They all seem to be the same to me, they take an implementation, encapsulate it and then methods are called on the wrapper/proxy/facade class that call the encapsulated object's methods.

Please show why they are different with examples.

Thanks

Jon
  • 38,814
  • 81
  • 233
  • 382
  • 2
    see also http://stackoverflow.com/questions/350404/how-do-the-proxy-decorator-adaptor-and-bridge-patterns-differ – yegor256 Sep 07 '12 at 06:34
  • [The Difference Between An Adapter And A Wrapper](http://www.thecodedself.com/The-Difference-Between-an-Adapter-and-a-Wrapper/) and [Design Patterns – Adapters and Wrappers](http://weblogs.foxite.com/andykramek/2007/01/07/design-patterns-adapters-and-wrappers/) – Curiosity Jul 21 '17 at 10:02

3 Answers3

73

The difference is mostly in the intent. Ultimately, they all do "take an implementation and wrap it", but it's important to communicate the difference.

The wrapper pattern (aka the adapter pattern) takes one interface and adapts it to the other.

interface A { void Foo(); }
interface B { void Bar(); }

class AAdapter : B { 
   private A a;
   public AAdapter(A a) { this.a = a; }

   void Bar() {
      a.Foo(); // just pretend foo and bar do the same thing
   } 
}

A proxy implements an interface for the purpose of providing access to something else (usually something big). A good example is remote procedure calls.

interface PiCalculator {
    double CalculatePi();
}

class Ec2PiCalculatorProxy : PiCalculator {
    public double CalculatePi() {
       // Fire up 10000 of computers in the cloud and calculate PI
    }
}

We call it a proxy rather than a wrapper to communicate that it's tunnelling through to another component to fulfil the results. I don't see this the same as the adapter pattern, because that's about converting interfaces.

A façade differs because it hides the collaboration of multiple classes behind a simpler interface or class.

class Facade {
  private A a;
  private B b;

  // Provides an interface to A and B by delegating to these members  

  public void DoSomethingWithAAndB() {
    MagicToken x = a.DoSomethingAndGetAResult();
    b.DoSomethingWithMagic(x);
  } 
}
Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
  • How would you describe this approach if it wasn't using RPC, because at the moment it is therefore a Proxy but imagine its just encapsulating one object with the ability to dispose of the encapsulated object. http://stackoverflow.com/a/12259840/84539 – Jon Sep 06 '12 at 11:26
  • I think I'd probably still call it a proxy, since it is proxying the resource management of another object. As you've hinted at with your question, these aren't hard and fast rules. I don't think there is a huge difference in implementation, but there is one of intent. – Jeff Foster Sep 07 '12 at 07:51
  • If Facade collaborates between objects, then what does Mediator does?I think Facade just wraps different interfaces and pass the client request to correct interface. Correct me if I am wrong. – AKS Sep 30 '13 at 22:50
44

Many design patterns have the same structure, as you have seen.

The difference is in the reason for their existence - the why of their existence.

A proxy is there to act as a local object representing a remote one.

A wrapper is there to wrap an existing object to extend/change its behavior.

A façade exists to simplify a complex API and expose a simple one instead.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Isn't your definition of wrapper here the decorator pattern? – Jon Sep 06 '12 at 09:15
  • @Jon - What's the difference? – Oded Sep 06 '12 at 09:18
  • 4
    I don't know that's why I asked the question – Jon Sep 06 '12 at 10:20
  • 7
    Are there a pattern named `Wrapper`. Isn't it just a definition of anything that wraps an object to make it behave slightly different (that is a wrapper can be a decorator/facade/proxy/adapter)? – jgauffin Sep 06 '12 at 12:06
  • 2
    @jgauffin - That's my impression too. I am not aware of a "Wrapper" design pattern. – Oded Sep 06 '12 at 12:37
  • 1
    I have always thought "Wrapper" is merely common coder slang for what is often the _structure_ of {facade, proxy, adapter, provider, bridge, decorator,...}. – M.Bearden Jul 12 '18 at 22:55
9

AFAIK there is not a pattern called wrapper. Seems more like a generic definition of one of the behavioral patterns.

Facade

Do not wrap a single class but several. The intent is to make a complex API easier to work with. A .NET example is the WebClient class which uses HttpWebRequest/HttpWebResponse

Proxy

Proxies acts as the original object but contains additional logic such as handling a connection or cleaning up resources when done.

If you are using WCF you can generate a client proxy by supplying the WCF service interface.

Additional patterns

There are some more patterns which also is "wrappers":

  • Adapter
  • Decorator
jgauffin
  • 99,844
  • 45
  • 235
  • 372