Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

So far as I know, there are two forms of the Adapter Pattern: the class adapter and the object adapter. Could someone please explain to me the following limitation of the class adapter and why it is not a limitation of the object adapter:

  • a class adapter is not suitable when we want to adapt a class and all of its subclasses.

I guess the following is considered a limitation of the object adapter:

  • with an object adapter it is harder to override Adaptee behaviour than with a class adapter.

Why is that?

share|improve this question

1 Answer 1

A class adapter IS a subclass, so you certainly shouldn't use it to adapt multiple subclasses. Even assuming that multiple inheritance is available in your language of choice, this would create an unmaintainable chain of inheritance.

An object adapter wraps an object and thus can't be used to modify the behaviour of the class of the object it's wrapping. It can, however, decide how to use that behaviour.

share|improve this answer
    
could you give an example of an unmaintainable chain of inheritance I don't quite follow, please forgive me –  user128833 May 15 at 13:18
    
Are you familiar with the diamond problem with multiple inheritance? If you have multiple subclasses, it becomes multiple diamonds linked together. I like to think of it as a rope ladder. This occurs several places with multiple inheritance (even with interfaces like in Java/C#) and can make a class hierarchy really difficult to grok. –  Snowman May 15 at 15:15
    
grok- cool word. I think I get it, thanks. –  user128833 May 21 at 3:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.