Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

When to Design an Inteface

 
Ranch Hand
Posts: 54
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've read the FAQ: InterfaceVSAbstract

I'm mainly curious when you are designing a new piece of software what is it that tell you do design an Interface vs just building a class and using it? I'm familiar with the use, but I'm still new at designing my own pieces of software and still am just not sure what I should look for as to when I should design and Interface to Implement vs a class to use...

Again, I'm aware of HOW I just don't have the WHEN...
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you want to ensure that every implementer actually implements the methods in the interface.

For example, if you wanted every IMPLementer Class to log or other common methods.

WP
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a rough guideline, a good starting point is, "Is this a very specialized type that only has this one particular useful form? Or is this a higher-level generality that might take different forms in how it's actually implemented, depending on which specific kind of use-case it's supposed to handle.

For instance, I recently wanted to be able to represent ranges, such as 1-5, 10-20 without having to specifically list every element, but still be able to iterate over all the elements, determine how many elements it had, determine whether a particular value was included in that range, etc. I could have just defined a Range<T extends Number> class and done simple arithmetic to realize that functionality. However, at about the same time, I also had a use for specifying ranges of IP addresses the same way, such as 192.168.0.* or 192.168.0.100-200. The operations I would want to do in both cases were the same, but the simple math that work for a range of numbers wouldn't work for IP addresses.

So I defined an interface Range<E extends Comparable<E>> extends Iterable<E>. There are one or two sub-interfaces, a couple of abstract classes, and a couple of concrete classes. I can now do stuff like:

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William P O'Sullivan wrote:When you want to ensure that every implementer actually implements the methods in the interface.



That doesn't really make any sense. If you just want to ensure that all the methods are implemented, you can create a concrete class.

The point is that when you want there to be able to be multiple implementations, and there isn't any one "default" umbrella that could reasonably encompass them all.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is an example i recently wrote for solving project Euler problems


all problems are solved using a class that implements Problem. this allows you to treat them all the same.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jared Malcolm wrote:I'm mainly curious when you are designing a new piece of software what is it that tell you do design an Interface vs just building a class and using it?...
Again, I'm aware of HOW I just don't have the WHEN...


I think my advice would be to always think about an interface first.

If, as Jeff says, you find that the type is highly specialized and is only ever likely to have one implementation, then you can always drop the interface in favour of a single concrete class; it's more difficult (and sometimes impossible) to retro-fit an interface to an existing class if you find out later that you have an alternative implementation.

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic