适配器模式:类适配器与对象适配器 [英] Adapter Pattern: Class Adapter vs Object Adapter
问题描述
什么时候可以使用类适配器对象适配器,反之亦然此外,使用类适配器和对象适配器的权衡取舍是什么?
我可以看到对象适配器的一个优势,具体取决于您的编程语言:如果后者不支持多继承(例如Java),并且您想要适应几个适配器,您将无法使用对象适配器。
对象适配器的另一点是,您可以让包装的适配器按需要生活(实例化,只要您在适配器之后实例化适配器),而不必在实例化适配器时指定所有参数(适配器的部件以及适配器的部分,因为继承)。这种方法对我来说更加灵活。
I have a few questions about the Adapter pattern. I understand that the class adapter inherits from the adaptee while the object adapter has the adaptee as an object rather than inheriting from it.
When would you use a class adapter over an object adapter and vice versa? Also, what are the trade-offs of using the class adapter and the trade-offs of the object adapter?
I can see one advantage for the object adapter, depending on your programming language: if the latter does not support multiple inheritance (such as Java, for instance), and you want to adapt several adaptees in one shot, you'll be obliged to use an object adapter.
Another point for object adapter is that you can have the wrapped adaptee live his life as wanted (instantiation notably, as long as you instantiate your adapter AFTER your adaptee), without having to specify all parameters (the part for your adapter AND the part for your adaptee because of the inheritance) when you instantiate your adapter. This approach appears more flexible to me.
这篇关于适配器模式:类适配器与对象适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!