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

前端 未结 3 1131
野的像风
野的像风 2021-01-30 03:05

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

相关标签:
3条回答
  • 2021-01-30 03:43

    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
    0 讨论(0)
  • 2021-01-30 03:44

    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);
      } 
    }
    
    0 讨论(0)
  • 2021-01-30 03:51

    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.

    0 讨论(0)
提交回复
热议问题