Adapter Pattern vs Proxy Pattern – Key Differences

design-patterns

As far as understand, the adapter pattern is creating a wrapper object for our real object of interest, simply one more level of indirection, which provides flexibility. the flexibility is in that, if the real object's interface is changed, then we change the wrapper interface pointing at the real object, leaving the client-side exposed interface unchanged.

The proxy pattern is the same, with the difference that every proxy wrapper provides only a coherent subset of the real object's functionality. Why would this be useful, when we strive to make "one class for one purpose" is beyond me.

Have I gotten this correctly?

Best Answer

Not entirely.

The primary purpose of the adapter pattern is to change the interface of class/library A to the expectations of client B. The typical implementation is a wrapper class or set of classes. The purpose is not to facilitate future interface changes, but current interface incompatibilities.

The proxy pattern also uses wrapper classes, but for a different purpose. The purpose of the proxy pattern is to create a stand-in for a real resource. Reasons for using a proxy can be

  • The real resource resides on a remote computer (the proxy facilitates the interaction with the remote resource)
  • The real resource is expensive to create (the proxy ensures the cost is not incurred unless/until really needed)

The most important thing is that a proxy provides a drop-in replacement for the real resource it is a stand-in for, so it must provide the same interface.

Related Topic