3

我必须访问其中包含“通用”表的旧数据库,并且我无权更改它。根据我正在使用的客户数据,表之间的关系可能会有所不同。因此,customerA 可以仅通过其客户编号加入订单表,而 CustomerB 可以通过客户编号和日期加入订单表。CustomerC 可能根本不加入订单表,而是加入不同的表。

所以,我想做的是为 CustomerA 创建一个对象图,为 CustomerB 创建一个对象图,为 CustomerC 创建一个对象图。我考虑为每个创建一个包装类,但一直在研究代理。也就是说,关于代理类的示例使它们看起来与包装类相同。因此,我的问题是代理类是包装类的同义词。

谢谢你。

4

4 回答 4

3

不,它们不一样。在 Wikipedia 上查看Proxy 模式Wrapper 模式。

基本上,代理是一个看起来与原始对象相同的对象(即实现相同的接口),但功能更多。

Wrapper 是一个看起来与原始对象不同的对象,但通常做相同的事情。

于 2011-07-18T21:01:44.017 回答
2

有几种方法可以处理手头的问题。

一种是映射到通用域模型。如果您具有相同的基本行为,这可以正常工作,但在细节方面可能无法很好地为您服务(不同类型的客户端的不同键)。

Another is to move the common bits down into a base class and then inherit for the different specifics in state (different properties) or the different behaviors (primary key only id, etc). Considering this is both differing behavior and state, this is a direction you can use.

Now, patterns. A proxy pattern is one where the new object can provide some of the behavior and state of another object, but is not the object. Think of it like what a person voting for you as a proxy means, and then relate it to software. I would not think of it like a wrapper, as a wrapper is normally used to present a different face from the underlying object. This can be due to the need to hide something in the underlying object or to add further behavior on top of the object. The proxy pattern will not help with the varying keys; Possible with the wrapper, although I am not convinced it is the simplest way to do this.

于 2011-07-18T21:05:05.940 回答
1

来自维基百科:“代理,以其最一般的形式,是一个类,作为其他东西的接口”

代理模式

代理:是一个“接口”,它是一个“黑匣子”,如果你愿意的话,你可以从中请求一些东西,它会给你你想要的东西。

Wrapper:是一个实体,它基本上通过封装来隐藏功能,因此在其中隐藏一个“真实”组件/另一个类/...通过向调用者公开它自己的方法,因此 wrapper 的用户不知道它是哪个对象真的有效。

希望这可以帮助。

问候。

于 2011-07-18T21:04:20.760 回答
0

查看代理和包装类的实现可能非常相似。然而,这些术语经常在不同的场合中使用。

代理是一个行为类似于真实对象的对象,但不是。相反,它将所有调用转发到真实对象,隐藏了访问远程对象的复杂性。代理对象的一个​​示例是由 Visual Studio 生成的 WCF 客户端。客户端调用它们,就好像它们是真正的服务代码一样,代理处理通信。

包装器是由于某种原因隐藏另一个对象的对象。通常这是在接口不兼容时完成的。具有正确功能但接口错误的对象被包装在另一个转换接口的对象中。

于 2011-07-18T21:02:27.603 回答