什么是代理,包装或外观类之间的差异 [英] What are the differences between proxy, wrapper or a façade classes

查看:227
本文介绍了什么是代理,包装或外观类之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是代理,包装或外观类之间的差异

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.

感谢

推荐答案

的区别主要是在意向。最终,他们都做采取实施,敷,但沟通的差异是很重要的。

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);
  } 
}

这篇关于什么是代理,包装或外观类之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆