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
The wrapper pattern (aka the adapter pattern) takes one interface and adapts it to the other. A proxy implements an interface for the purpose of providing access to something else (usually something big).
The proxy pattern is where one object acts as an interface to another object. The difference between proxy and facade patterns are that we can proxies create interfaces for one object. But facades can be interfaces for anything. We use the proxy to call both foo and bar in obj .
In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic.
Adapter and Facade are both wrappers; but they are different kinds of wrappers. The intent of Facade is to produce a simpler interface, and the intent of Adapter is to design to an existing interface.
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With