PrettyPrincessKitty FS
PrettyPrincessKitty FS

Reputation: 6400

Programming against an interface with only one class implementing said interface

I can understand why to program against an interface rather than an implementation. However, in an example like the following (I find this a lot):

public interface ISomething
{
    void BlahOne(int foo);
    void BlahTwo(string foo);
}

public class BaseSomething : ISomething
{
    public void BlahOne(int foo)
    {
        //impl 
    }

    public void BlahTwo(string foo)
    {
        //impl
    }
}

public class SpecificSomethingOne : BaseSomething
{
    public void SpecificOne()
    {
        //blah
    }
}

public class SpecificSomethingTwo : BaseSomething
//and on..

The current example of this is the component based entity system in my game. (I have IComponent, Component, PosComponent, etc).

However, I cannot see a reason to have ISomething. The name may look nicer, but it doesn't seem to have a purpose. I can just return BaseSomething all the time.

Is there a reason to have an interface when you have a single base implementation everything uses? (I can see the use for, say, IComparable or IEnumerable)

EDIT: For a slightly different scenario (yet still related enough to not need a different question), if I assume I have this structure for everything, would there be much difference if I were to use ISomething for parameter types and variables compared to BaseSomething?

Upvotes: 3

Views: 1965

Answers (4)

Anders Abel
Anders Abel

Reputation: 69270

I prefer "lazy design" - extract the interface from BaseSomething when you need it. Until then, keep it simple and skip it.

Right now I can think of two reasons for having an interface when there is only one implementation:

  • There is another mock implementation for unit tests (i.e. there is a second implementation, although not in production code).
  • The interface and the implementation are defined in different class libraries. E.g. when using the Model-View-Presenter pattern, the view can reside in an .exe project that is dependent on the .dll where the presenter is implemented. Then an IView interface can be put in the .dll and the presenter's reference to the view supplied through dependency injection.

Upvotes: 13

this. __curious_geek
this. __curious_geek

Reputation: 43217

Correct answer to your question would be "It depends".

You can look at it in many different ways and it's all about perspective. When you have a concrete or abstract base class, it means your objects have something in common functionally. And derived objects are inter-related in some way or the other. Interfaces let you confirm to a functional contract only where each object implementing the interface will be responsible for the implementation.

Again, when you program again interfaces, you strictly know the capabilities of the object since it implements the given interface. And you need not worry about how each object functionally implements this.

It'd not be completely wrong, If I say each of your objects are completely independent when it comes to implementing the interface ISomething, given that SpecificSomethingOne and SpecificSomethingTwo do not derive from BaseSomeThing and each implement their own ISomething.

You can refer to this answer on the same matter.

Upvotes: 2

Fourth
Fourth

Reputation: 9351

If your BaseSomething were abstract and you had implementing specific things that provider overloads to abstract methods, the only way to program to them at that point would be to the ISomething interface. However, in the example you showed, there is really no reason for ISomething unless you could have multiple base implementations.

Upvotes: 0

thomas
thomas

Reputation: 5649

it is not really necessary but it is a better design if you want to extend your program later or you want to implement another Base-Class. In your case I would not implement the Base-Class. The Interface only is just fine if you dont want to have a default-behaviour. If you want a default-behaviour then just write the Base-Class without an Interface

Upvotes: 1

Related Questions