代理,包装器或façade类之间有什么区别

时间:2022-09-25 12:15:00

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

代理,包装器或façade类之间有什么区别

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

谢谢

3 个解决方案

#1


40  

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

#2


21  

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.

存在简化复杂API并展示简单API的外观。

#3


6  

AFAIK there is not a pattern called wrapper. Seems more like a generic definition of one of the behavioral patterns.

AFAIK没有一种叫做包装的模式。似乎更像是一种行为模式的通用定义。

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

不要包装一个类,而是几个。目的是使复杂的API更易于使用。 .NET示例是使用HttpWebRequest / HttpWebResponse的WebClient类

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.

如果您使用的是WCF,则可以通过提供WCF服务接口来生成客户端代理。

Additional patterns

其他模式

There are some more patterns which also is "wrappers":

还有一些模式也是“包装”:

  • Adapter
  • 适配器
  • Decorator
  • 装饰

#1


40  

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

#2


21  

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.

存在简化复杂API并展示简单API的外观。

#3


6  

AFAIK there is not a pattern called wrapper. Seems more like a generic definition of one of the behavioral patterns.

AFAIK没有一种叫做包装的模式。似乎更像是一种行为模式的通用定义。

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

不要包装一个类,而是几个。目的是使复杂的API更易于使用。 .NET示例是使用HttpWebRequest / HttpWebResponse的WebClient类

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.

如果您使用的是WCF,则可以通过提供WCF服务接口来生成客户端代理。

Additional patterns

其他模式

There are some more patterns which also is "wrappers":

还有一些模式也是“包装”:

  • Adapter
  • 适配器
  • Decorator
  • 装饰