什么是包装器类? 这样的类有什么用处呢?


当前回答

有几种设计模式可以称为包装器类。

请参阅我对“代理、装饰器、适配器和桥接模式有何不同?”

其他回答

顾名思义:一个类将另一个类或API的功能“包装”在一个更简单或只是不同的API中。

参见:适配器模式、门面模式

In general, a wrapper class is any class which "wraps" or "encapsulates" the functionality of another class or component. These are useful by providing a level of abstraction from the implementation of the underlying class or component; for example, wrapper classes that wrap COM components can manage the process of invoking the COM component without bothering the calling code with it. They can also simplify the use of the underlying object by reducing the number interface points involved; frequently, this makes for more secure use of underlying components.

包装器是用于在不同平台之间的两个不同应用程序之间进行通信的类

包装器类通常是一个具有对象作为私有属性的类。包装器实现了私有对象的API,所以它可以作为私有对象的参数传递。

假设您有一个集合,并且希望在向其中添加对象时使用某种转换—您编写一个具有集合的所有方法的包装器类。当调用add()时,包装器转换参数,而不是仅仅将它们传递到私有集合中。

包装器可以在任何可以使用集合的地方使用,私有对象仍然可以有其他对象引用它并读取它。

包装器类是一个包装另一个类并提供客户端和被包装的原始类之间的抽象的类。