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

