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

由网友(ご伱我dē寳)分享简介:什么是代理,包装或外观类之间的差异What are the differences between proxy, wrapper or a façade classes他们都似乎是一样的我,他们采取的实现,封装,然后调用方法在调用封装对象的方法包装/代理/门面类。They all seem to be the s...

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

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);
  } 
}
阅读全文

相关推荐

最新文章