wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Question about programming to an implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Question about programming to an implementation" Watch "Question about programming to an implementation" New topic
Author

Question about programming to an implementation

Tontang Bei
Ranch Hand

Joined: Oct 21, 2006
Posts: 130
On page 16 of HFDP, the code

Is the above called programming to an interface or programming to an implementation?.

on page 12, programming to an interface is shown as:

So both codes seem to be 'programming to an interface' as Animal is superclass of Dog and QuackBehavior is interface or Quack subclass. Then why is the girl on page 17 complaning that we have programmed to an implementation???
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I don't have the book so I can't be sure, but both of those code snippets create concrete classes. They are tightly bound to Quack, FlyWithWings and Dog. Consider a class like this:

This is coded entirely to interfaces with no references to concrete classes. You can do the same with method arguments:

We can use these snippets with any current or future Animal without touching this code. Does that sound like what the book is about?
[ January 11, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Tontang Bei
Ranch Hand

Joined: Oct 21, 2006
Posts: 130
Thanks,
but Im still a bit unclear,
What would you say is the code below an example of:
1.)Animal animal=new Dog(); //Is this programming to an interface/supertype?

And what about this:
2.)quackBehavior=new Quack(); //?

quackBehavior is an interface, and Quack is a class that implements it.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I reckon most people would consider your Animal/Dog example as programming to an interface. By declaring your variable as type Animal, you are ensuring your code treats the object as an Animal, and does not use any methods only present in Dog. In particular, if you wanted to use a Cat instead of a Dog, you know you can do it by changing just this one line of code to something like:



Stan kind of skipped this step and jumped straight to the next one. In modern designs it is becoming less usual to call "new" inside domain methods. Istead we prefer to "inject" (also known as "pass in") pre-created objects. In Stan's example our code knows nothing at all about the type of the animal object, except that it implments Animal.

In your other example, you haven't really shown enough to tell if you are programming to an interface or not. You only tell us the name of the "quackBehaviour" variable, but not its declaration.

If the declaration is something like



then you are probably coding to an interface, although the name seems a poor choice. If you were to replace "new Quack()" to "new Bark()", your code would now look like



which does not make much sense.

If (as seems more likely from the name), the declaration of your variable looks more like:



Then you are coding to an implementation. You can tell this because if you try and replace "new Quack()" with "new Bark()", you get a compiler error. A Bark is not a Quack.

Does that make any more sense?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
perry forman
Greenhorn

Joined: Jan 11, 2005
Posts: 7
Even thou it appears that Quack is a concrete implementation, it is also an interface in this example.

They have defined several types of Quacking, i.e. Silent (in case of wooden duck), Squeak (in case of rubber duck), etc. All these types implement a howToQuack interface.

I don't have the book in front of me, but I believe "quackbehavior" is defined above as a variable of the howToQuack interface.
 
wood burning stoves
 
subject: Question about programming to an implementation
 
Similar Threads
Creating Objects
Did I make the Strategy pattern unnecessarily complicated?
Problem with Head First Design Patterns code
downcasting doubt
I'm probably overlooking something very stupid...