When do you use an interface?

  • Thread starter Thread starter Andy B.
  • Start date Start date
A

Andy B.

Was reading up on interfaces and they look like a good thing. Just wondering
when do you use an interface (decide to create one and use it) rather than
leaving interfaces out of the design? Is it a matter of preference?
 
Was reading up on interfaces and they look like a good thing. Just wondering
when do you use an interface (decide to create one and use it) rather than
leaving interfaces out of the design? Is it a matter of preference?

I have heard that designing your project to implement an interface
helps to make unit testing easier, but I have yet to see how that
works.

I am currently woirking on an app at work that is a general UI that
loads various "plug-ins" (DLLs) to redefine how the UI looks and
functions. Each plugin implements a base interface. In this case it
kinda helps to make sure each plugin performs in basically the same
way.
 
Andy said:
Was reading up on interfaces and they look like a good thing. Just
wondering when do you use an interface (decide to create one and use
it) rather than leaving interfaces out of the design? Is it a matter
of preference?

If there's nothing to implement, write an Interface.


Armin
 
Was reading up on interfaces and they look like a good thing. Just wondering
when do you use an interface (decide to create one and use it) rather than
leaving interfaces out of the design? Is it a matter of preference?

My most common use of Interfaces is as a substitute for multiple
inheritance.

Suppose I have two classes that are very different, such that it makes
no sense to derive one from the other. Yet they do share some
functionality. If I have both implement an Interface, then I can cast
an object of either type to the Interface and call the Interface's
methods and properties.

A good example of this in the framework is IDispose. It doesn't make
sense to implement this in the basic Object, yet many classes need to
implement its functionality.
 
Hello Andy

I create interfaces when i want to make sure that a object implements at
least a certain signature
for instance in the company i work for i created a BL service wich can start
anny object with custom code as long as it has my interface implemented
so all four coders in my company implement my interface and then write there
own code around it and it can be started by my service

So as i said before a interface is like a contract , you conform to at least
implementing the method signature in your project so you provide a generic
interface
wich may be verry handy in business programming ( and ofcourse it saves you
a lot of typing ) .

regards

Michel Posseth
 
Its a very loaded question...as in...simple sentences don't often explain it
very well.


http://www.dofactory.com/Patterns/Patterns.aspx

I would look there, I wish someone had told me about Design Patterns 6-7
years ago, instead of 4 years ago.

Look at the observer pattern first, its one that good to grab and see.


Coding to interfaces takes longer, but pays off in maintenance.
It also opens up things like design patterns to you. Ways to make
better/smarter code.
 
Andy,

As less as possible, in my idea they don't add anything to the design,
however as you need them, then use them.

Cor
 
Back
Top