Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt regarding Design principle  RSS feed

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

While reading Head first design pattern ,i came accross a principle "Program to interface not implementation",i could not understand it ,Can any one please elaborate it.
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the simplest level it means to make your code depend on interfaces whenever you can. Say we write this:

Now somebody else can call our method with an ArrayList or a LinkedList or some new implementation of the List interface that we never heard of. If we had made that parameter a LinkedList somebody with an ArrayList wouldn't be able to calls us. Using the List interface our code is more likely to be useful to more other people over the years.

At a slightly more complex level, that principle talks to the folks who wrote List. They gave us an interface so they are free to create new implementations without breaking our code. So if you write some new thing that you'd like people to use and there is any chance you might come up with another implementation or if you'd like to invite the world to come up with their own implementations, publish an interface.

For one more example, look at the javax.sql package. It's almost all interfaces. Sun didn't provide a Connection or a ResultSet implementation in the library. It's up to the database vendors - Oracle, IBM, MySql and so on - to provide those. When you and I use databases we code almost entirely to interfaces. Cool.

Does that answer the right question?
[ April 24, 2007: Message edited by: Stan James ]
 
Harish Tiruvile
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan James,

Thank you very much for providing valuable information.

One more doubt .

in Head first book they told ," Take what varies and ENCAPSULATE it so it wont affect the rest of your code"



About encapsulation So far i used to think ,it is hiding variable my using private modifiers and providing access to those variable through getter and setter methods .But while reading head first book ,i realised there is some think more in encapsulation concept .

While explaining Stratergy pattern they separated thinks which varies ,and
created new interface\class which provide implementation for those which
varies implementation.

Does Creating more specific class will also be called as encapsulation?

Stan James ,can you please make it clear to me

Thanks ,
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We should maybe start a new thread, but what the heck ...

Surprisingly, there are about as many definitions of "encapsulation" as there are bloggers and books. I've worn my definition down to the point where it is little more than a new name for "information hiding" which has been around since the early 70s.

The bottom line is when you make a class, let other classes know as little as possible about how it works. The advice to hide "what varies" is pretty neat. Here are two definitions of "varies" ... things that change over time and things that vary from one implementation to the next ...

If you expose something to other classes, they will come to depend on it, and you won't be able to change it without breaking those other classes. If you hide the details of a class and expose only the most important behavior, you can change the details without telling anyone.

Strategies expose a common method for behavior, but hide how they work. That lets you swap strategies in and out, like plug and play.

Cool, no?
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Commonality-variability analysis
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!