tags:

views:

236

answers:

8

Why are we implementing, for example ICloneable,IDisposable. Actually I am not asking what ICloneable or IDisposable do, but I want to learn what's a good reason to implement these interfaces rather than just writing a method which dispose or clone our objects ?

Thanks.

+12  A: 

Using interfaces keeps the use of those pieces of functionality consistent. That way, when another class wants to / needs to use your class, it can act on it as a cloneable, disposable object without worrying about your particular implementation details.

phoebus
I was thinking the same thing sometimes but I wasn't quite sure because there would be something else, But I was kinda right about my thought :) Thank you very much.
Braveyard
+1  A: 

Among other reasons, read about the using block.

bablo
I think it is used for disposing the un-referenced objects.But I wasn't asking that :) well thanks for your time :)
Braveyard
+3  A: 

Because you may want the same code to operate on instances of different classes. For example, a form cleanup routine wants to iterate over all components and dispose them. In order for it to do this, it needs to refer to the components through a type: either a common base class or an interface. Single inheritance means that a common base class isn't always feasible (suppose my form has both a FileStream and a Button -- what common base class could they have that the cleanup routine would access them through?); hence interfaces.

itowlson
+5  A: 

By implementing a well known interface, you can have polymorphism, which enable you to write generic code that can act on any instance of a class that implementes a given interface.

You can check the Wikipedia article on polymorphism for more.

pgb
+1  A: 

Phoebus gave a great answer, but to add a bit more.

It forces you to write the methods that are expected for cloning or making something that can be disposed. If you had to write just the method, would you always include all the necessary methods, or would the signature always be the same?

If there is a new framework, and they add something, to help ensure that the operations are done correctly, by having interfaces then it forces you to implement any new changes, as your application will no longer compile.

James Black
+1  A: 

In summary the Interface separates the implementation and defines the structure, and this concept is very useful in cases where you need the implementation to be interchangeable. Apart from that an interface is very useful when the implementation changes frequently.

Interface can be used to define a generic template and then one or more abstract classes to define partial implementations of the interface.

Interfaces just specify the method declaration (implicitly public and abstract) and can contain properties (which are also implicitly public and abstract). Interface definition begins with the keyword interface. An interface like that of an abstract class cannot be instantiated.

egyamado
+2  A: 

Adding to more interface wisdom, interfaces can be used as contracts between consumer code and service code. Instead of saying this is object that we will be dealing with, with interfaces the agreement is, this is how the object i return looks. In code, it could be something like:

service code

public IEnumerable<int> GetNumbers(){ return new []{1,2,3,4,5}; }

client code :

var result = serviceProxy.GetNumbers();

Here the service code can change implementation to return any class that satisfies IEnumerable without breaking client code.

Besides all this you have got other applications like IoC DI, Unit Testing, Object Mocking. All this reaping benefits of polymorphic goodness.

Perpetualcoder
A: 

An interface separates what's done from how it's done. Clients deal with the interface type, without having to know what the implementation class is, because they can rely on the contract it enforces.

This is important for situations that generate dynamic proxies. Clients need not know that they're dealing with a proxy, which gives you the freedom to inject whatever behavior you need. It's a common technique for aspect-oriented programming.

duffymo