spillbean:Can you be a little descriptive
All too often, I'm way too descriptive... But sure!
spillbean:I want to know whether I can simultaneously avoid all the methods of an interface to be implemented and prevent the class to be abstract.
As mentioned, no. But... As someone once said "these are not the droids you are looking for", perhaps. You really have two major options:
- Implement the interface in a base class, but have all (or most, whatever) of the implementations throw a NotImplementedException. Then you can derive from that concrete base class and override whatever you need to actually implement. This is a rather dangerous course, because you never know when the flow of control will reach one of this NotImplementedException methods and your app blows up in production. I'd probably not recommend this course of action unless you have no other choice (i.e. you did not define the interface, and you can't change it).
- Split up the interface in smaller parts. In a good desing, an interface describes a rather generic ability that is likely or at least conceivable to be shared by many rather different types. For example IXmlSerializable describes the rather generic ability to be serialized as XML. IEquatable the rather generic ability to be compared. So, you should strive to think of interfaces as such capabilities. If this does not fit, an interface may not be the droid you are looking for. If the capabilities of your 30-method interface still fall into such generic capabilities, it's rather unlikely that it really is one capability. It's more likely to be a composition of maybe 5-15 capabilities, and then you should instead define discrete smaller interfaces and then you only need to implement the relevante interface(s) for your concrete classes.
spillbean:What benefit will I get by splitting up the interface
Apart from a cleaner and more easily maintained design, you'll gain the benefit you're looking for - the ability to only implement a few of the 30 methods and still get a concrete implementation of your class.
As an example, let's assume you have the following:
public interface ICreature
{
void MakeSound();
int Height { get; }
BitMap Picture { get; }
} This is a hypothetical interface of a creature in a role playing game. Looks ok, but... Is the capability to render oneself as bitmap really integral to the concept of a creature? I'd rather split this interface:
public interface ICreature
{
void MakeSound();
int Height { get; }
}
public interface IVisible
{
BitMap Picture { get; }
}
Now I can choose to implement one, the other or both of the interfaces in my concrete classes.
I'd probably continue and factor out the ability to make sound as well in a IAudible interface.
For common combinations, you can always combine interfaces since an interface may inherit from more than one interface, i.e.
public interface IInteractiveCreature : IVisible, IAudible
{
int Height { get; }
}
Svante
AxCrypt - Free Open Source File Encryption & Online Password Manager -
http://www.axantum.com[Disclaimer: Code snippets usually uncompiled, beware typos.]
______
Don't forget to click "Mark as Answer" on the post(s) that helped you.