Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

13. Programming Principles > Programming to Interfaces

Programming to Interfaces

The basic technique for implementing dependency inversion using interfaces is called PROGRAMMING TO INTERFACES, and it’s always a good way to protect yourself (and your classes) from change. I’ve actually seen it described as “a fundamental principle of OOP”. It’s a good practice, but I personally doubt it’s fundamental, not in the way that, say encapsulation is.

The thing to be careful of with programming to interfaces is a cluttered class hierarchy. You should beware of any interface that is only implemented by one or two concrete classes. It’s probably just added complexity (otherwise known as “noise”).

This Is How Things are Usually Done

image

The Safe Way

image

Put on Your Thinking Hat

image

Knowing when to apply a design principle or pattern is just as important as knowing how, so here are some situations to think about. Assume that in each case your initial design has the


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial