Using Class Interfaces - Hannes's Mind Maze
Hannes's Mind Maze
Think smart!
Sign in
|
Join
|
Help
Home
Contact
About
RSS
Atom
Comments RSS
Search
Tags
.Net
.Net Framework 3.5
Analysis and Design
ASP.Net
Books
Business Analysis
Domain Driven Design
General
Hardware
Humor
I.T.
New South Africa
Personal
Process
Programming
Security
Software
Software Development
Sport
System Analyst
Technology
Navigation
Home
Careers
Downloads
Support
Archives
October 2008 (3)
June 2008 (1)
May 2008 (1)
January 2008 (2)
November 2007 (4)
September 2007 (3)
August 2007 (10)
July 2007 (4)
June 2007 (3)
May 2007 (5)
April 2007 (12)
March 2007 (3)
February 2007 (8)
January 2007 (14)
December 2006 (6)
November 2006 (5)
October 2006 (6)
August 2006 (1)
July 2006 (1)
June 2006 (4)
May 2006 (2)
March 2006 (4)
February 2006 (4)
January 2006 (4)
December 2005 (1)
November 2005 (10)
October 2005 (7)
September 2005 (10)
August 2005 (5)
July 2005 (6)
June 2005 (11)
May 2005 (13)
April 2005 (15)
March 2005 (5)
February 2005 (7)
January 2005 (16)
December 2004 (1)
Book List
Applying UML and Patterns
Continuous Integration
Design Patterns
Domain-Driven Design
Object-Oriented Design Heuristics
Patterns of Enterprise Application Architecture
Programming Pearls
UML Distilled
The Pragmatic Programmer*
Applying Domain-Driven Design and Patterns*
UML 2.0 in a Nutshell*
Head First Design Patterns*
Herding Cats*
Object Oriented Systems Development*
Like to read
SA Developer
SA Architect
Joel on Software
Joel Reddit
The Daily WTF
Wikipedia
ScottGu's Blog
Slash Dot
Using Class Interfaces
The UML definition of an class interfaces is that's it used to describe the external visible behaviours of a class. Now my question, should a public class have an interface for it's exposed methods or not? I believe so but what's a good practice regarding the use of interfaces? Lets also say that everything is straight forward meaning no COM+, GAC, etc to simplify the problem.
Share this post:
email it!
|
bookmark it!
|
digg it!
|
reddit!
|
kick it!
|
live it!
Published Thursday, February 09, 2006 7:59 AM by
hannes
Filed under:
Analysis and Design
Comments
#
re: Using Class Interfaces
Thursday, February 09, 2006 2:01 PM by
roaan
Hmm.
I think the "academic" answer is that the class should have an interface. There are tools that evaluate your class according to "best practices" which will give you beter marks if your classes implement interfaces.
That said however I disagree. You should only use interfaces if there is a likelihood that you want to support multiple (simultaneous) implementations for a single interface. If an interface will not have more than one implementation then the class should not have an interface.
I have the following issues with having an interface for every class
- You are in essence doubling the number of files you have, and thus double the
amount of maintenance you have to incur
- If you add a method to the class, you must add it to the interface as well. This
seems pretty pointless if it is going to be the only class that implements the
interface
- IDE support is not that great for interfaces. Remember, since you have the interface
you must use the interface.
I.e.
Foo x= new Foo(); // Should NEVER do this
IFoo x= new Foo(); // Since you have the interface, use it
Unfortunately this means you pass IFoo around. If, in your code, you then ask the
IDE to "go to definition" of a method on this interface it will go to the interface, as
it should. This is painfull however since there is only one class and you would have
liked to go to the single implementation of this method.
If you have or will have more than one implementation for an interface, then yes go for it, but otherwise you're just making your life more difficult for absolutely no benefit.
#
re: Using Class Interfaces
Thursday, February 09, 2006 2:58 PM by
hannes
Good point you make about over complicating your solution with adding really unnecessary interfaces. The reason I like interfaces is because of the design--by-contract principal. But I only use it on exposed class-methods. It pointless to use it for protected class or class members.
Leave a Comment
Title
(required)
Name
(required)
Your URL
(optional)
Comments
(required)
Remember Me?
Enter the numbers above: