0

I'm trying to decide whether to extend a base interface with additional methods or create several independent interfaces. I have a Task interface that looks like this

interface Task
{
    public function name();
    public function run(array $args);
}

Now I want to add optional description and dependencies methods. Should I create a new DetailedTask interface like this?

interface DetailedTask extends Task
{
    public function description();
    public function dependencies();
}

This way I could choose to implement either the base Task interface for a basic task class, or the DetailedTask interface if I needed a description and dependencies. Alternately I could create separate interfaces for the extra methods like this.

interface Describable
{
    public function description();
}

interface TaskDependent
{
    public function dependencies();
}

According to the interface segregation principle, the second is better because it is more flexible. However, I don't ever anticipate using Describable and TaskDependent on anything besides a Task instance. The extra interfaces just seem to add extra complexity. Which technique should I use?

2

First of all, with your question details:

Now I want to add optional description and dependencies methods.

If you want an optional method, then you should not go for interface, because as per interface rules, you MUST need to define methods in implementation class. See PHP interface.

So according to that rule, your first solution will not work because if you define class that implements DetailedTask, then that class must define both method description and dependencies.

The second way will give you freedom to choose whatever interface you want to implement.

  • I understand that if I implemented the DetailedTask interface I would be required to implement all 4 methods. However I could still choose to implement the base Task interface without the extra methods. That's what I meant by "optional". Edit: I may have misunderstood you. If you meant that I would not be able to add just description or just dependencies, then that is a good point. – Justin Howard Mar 12 '15 at 5:05
  • Yes, by having both methods description and dependencies in single interface, you will always have to implement it even if your implementation class needs only one of them, while creating 2 separate interface will allow you to implement any of them as per your need. so you need to implement 3 at a time OR 4 if you want – Rupal Mar 12 '15 at 5:59
1

Always go for separate interfaces in this case. As the task part is mandatory and the second part is optional.

Interface always forces to override all the methods mentioned in it.

Now think if you don't want to use the optional part. Still you will have to override it. Even if it doesn't do anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.