什么是包装器类? 这样的类有什么用处呢?
当前回答
值得注意的是,在某些环境中,包装器类所做的大部分工作都被方面所取代。
编辑:
一般来说,包装器将对包装所做的事情进行扩展,而不关心包装的实现,否则,包装与扩展被包装的类没有任何意义。一个典型的例子是围绕一些其他服务接口添加计时信息或日志功能,而不是将其添加到该接口的每个实现中。
This then ends up being a typical example for Aspect programming. Rather than going through an interface function by function and adding boilerplate logging, in aspect programming you define a pointcut, which is a kind of regular expression for methods, and then declare methods that you want to have executed before, after or around all methods matching the pointcut. Its probably fair to say that aspect programming is a kind of use of the Decorator pattern, which wrapper classes can also be used for, but that both technologies have other uses.
其他回答
有几种设计模式可以称为包装器类。
请参阅我对“代理、装饰器、适配器和桥接模式有何不同?”
Java编程为每个基元数据类型提供包装类,用于将基元数据类型转换为包装类的对应对象。
让一个包装类变得健康并不是一件容易的事情。 了解包装器类是如何被其他人设计的也不是一件容易的事情。 因为这是想法,不是代码。 只有理解了理念,才能理解包装。
包装器类是一个包装另一个类并提供客户端和被包装的原始类之间的抽象的类。
顾名思义:一个类将另一个类或API的功能“包装”在一个更简单或只是不同的API中。
参见:适配器模式、门面模式