• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class form Adapter VS Object form Adapter

 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "Class form" and "Object form" often appears in GOF book. What does that mean by "Class form" and "Object form"?

As for the Adapter pattern, the class form is implementing two interfaces; the object form is using composite to delegate.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you already got it: the class form uses inheritance, whereas the object form uses composition. There isn't more to it, as far as I can tell...
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Louis Wang:
As for the Adapter pattern, the class form is implementing two interfaces; the object form is using composite to delegate.


Some languages (especially when GOF was first published) supported only single inheritance. Without multiple inheritance or interfaces you cannot implement the class form but as long as you have single inheritance you can still implement the object form.

You can sometimes see a variation of the "object form" in Java when a class doesn't implement an interface itself but lets one of it's (anonymous) inner classes do it.

Robert C. Martin's article on the Interface Segregation Principle (pdf) talks about the differences between the object form (separation through delegation) and class form (separation through multiple inheritance) of the Adapter.
[ April 19, 2007: Message edited by: Peer Reynders ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another big advantage to the object form: you can use the object form to adapt an object that has already been created. With the class form, you need to be able to control the instanciation of the original object, so that the adapter instance is created.

I regularly use the object form for that reason.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic