About Interface

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

Suppose you review your code and notice that a number of your domain objects
share similar properties and methods.
Let's say the objects Customer, Vendor, Manufacturer, SalesAgent and Product
all contain properties for Id and Name and methods for Get, Save and Delete.

In this case I can create an Interface with these Id, Name, Get, Save and
Delete and have these five classes
implement this Intarface.

Now to my question what advantage will this give me.
I can't right now see any advantages doing this but I do know that almost
everybody will tell me that's a good thing to do.

I hope somebody can tell me something about why I should do it.

//Tony
 
Hello!

What do you mean with this ? I don't really understand what you want to say
here ?
"> The main benefit would be that any code that uses your objects that share
those members, and which doesn't need _any_ of the other members of the
objects, could be rewritten to the interface, rather than having explicit
uses of the implementing types."

//Tony

Peter Duniho said:
[...]
In this case I can create an Interface with these Id, Name, Get, Save and
Delete and have these five classes
implement this Intarface.

Now to my question what advantage will this give me.
I can't right now see any advantages doing this but I do know that almost
everybody will tell me that's a good thing to do.

I hope somebody can tell me something about why I should do it.

There's no way to know for sure you _should_ do it, not with so little
information about your code.

The main benefit would be that any code that uses your objects that share
those members, and which doesn't need _any_ of the other members of the
objects, could be rewritten to the interface, rather than having explicit
uses of the implementing types.

But, if you don't have any code like that, declaring an interface and
using it with each of those classes wouldn't gain you anything.

Pete
 
Tony Johansson said:
Hello!

Suppose you review your code and notice that a number of your domain
objects share similar properties and methods.
Let's say the objects Customer, Vendor, Manufacturer, SalesAgent and
Product all contain properties for Id and Name and methods for Get, Save
and Delete.

In this case I can create an Interface with these Id, Name, Get, Save and
Delete and have these five classes
implement this Intarface.

Now to my question what advantage will this give me.
I can't right now see any advantages doing this but I do know that almost
everybody will tell me that's a good thing to do.

I hope somebody can tell me something about why I should do it.

//Tony

Hi,

Instead of extracting an interface, You might profit from extracting a
superclass with the common members in it (
http://refactoring.com/catalog/extractSuperclass.html ).

Hope You find this useful.
-Zsolt
 
Back
Top