Interfaces for your own classes - do you bother?

Hi All,

before anyone gets upset, I did post a shorter version this to the Java Prog forum a couple of days ago. I'm reposting here because the thread was pushed down the list just as comments were getting interesting, and I was tossing up which forum was most appropriate anyway...

OK, I'd like to pose a general question on the use of interfaces with your own classes. I am totally behind the concept of using appropriate interfaces for declaration of vars, parameters etc (i.e. use "List" rather than "Vector"), but what about your own classes?

If I have a class in my application that doesn't fit into any kind of framework or class hierarchy, subclass of object basically, should I write an interface with all the same method sigs and use that rather than the class for my vars, params etc? This would make things more flexible in future, enabling you to swap classes in and out of your app more easily.

Taking it a step further, how's this for a design methodology: For every class you write (once again classes that don't fit into your own or somebody else抯 hierarchy or framework) do the following

1. Create an interface (MyInterface) that maps 1 to 1 to that class (MyClass), with all the same method sigs.

2. Make the constructor private and manage all the creation of instances of MyClass through a factory that returns objects of type MyInterface.

If you do this there will be no mention of any of your concrete classes anywhere in your app except the factories, making it easier to refactor etc. Do this for all your classes.

A little sketchy but I hope you get idea. Is this complete overkill? Is there a down side to this? Does anyone do anything like this?

Cheers

Matt

[1763 byte] By [mattrumpus] at [2007-9-27 19:11:36]
# 1

To me, it's an issue of defining boundries and determining how tightly coupled you components need to be. When designing a new system, I will often use nothing but interfaces to define the system abstractly. It is only once those interfaces need to be implemented that tightly coupled classes will come about.

If you defined your boundries well to begin with, then any refactoring of implementation you do will only impact those classes who, by their very nature, are tightly coupled. If that is not their nature, then, yes, you might need to refactor your interfaces/boundries.

jcvanvorst at 2007-7-6 21:36:41 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Yeah,

I do something similiar. Tightly bound classes I treat a "module" and expose appropriate interfaces to have that module interact with the rest of the system.

I'm asking this question because I have recently come across someone who ONLY codes to interfaces.

cheers

matt

mattrumpus at 2007-7-6 21:36:41 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Although the pattern you describes adds flexibility, it also adds complexety.

Imagine somebody is looking at your code and from what he sees he deducts that the next thing to look at is the concrete class which is used at a given place.

If you used an interface, he must find out which implementing class is actually used at this place. It's not a big thing, but if every single class has its seperate interface and factory I would consider it overkill ... after all you have to write all the boring code for this.

In the case of doubt I'd agree it's better to throw in an interface and a factory instead of refactoring thousand lines of codes afterwards. But if nothing hints toward a second implementation of the interface, or another reason for using an interface don't bother to write one.

As a general rule I use something like: Don't use a general rule. ;-)

Spieler

spieler at 2007-7-6 21:36:41 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
Hi,I know there are no easy answers when it comes to design. Thanks for your input,matt
mattrumpus at 2007-7-6 21:36:41 > top of Java-index,Other Topics,Patterns & OO Design...