edc65
edc65

Reputation: 468

Different classes have to implement an interface method in exactly the same way

I have and interface defining a bunch of different properties and a State property that is a compact summary of all the other.

So I have a common interface named IStateful

interface IStateFul
{
    string State { get; set; }
}

I have some classes implementing this interface in different ways - one asks for user input, another read the values from a service, one could get data from a DB. But all of them have to compute the state in exactly the same way to be "compatible" with each other. So the implementation of State in the different classes have to use the other property in exactly the same way to compute the state string.

As of today I copy and paste the state implementing method from a class to the other. This is obviously the worst option.

Other options could be

There is a common pattern to follow? Or is there a basic design flaw that I'm missing to see?

Upvotes: 0

Views: 2150

Answers (3)

Pedro G. Dias
Pedro G. Dias

Reputation: 3222

I would define the interface IRequireState

public interface IRequireState
{
    bool GetState();
}

And use that for all classes requiring state. There is a programming principle called "The Interface Segregation Principle" (ISP) which basically states that you should ONLY implement an interface in the classes where it makes sense to do so.

According to the Open Closed Principle you can always subtitute a derivative, so for example, if you have 3 classes, all implementing the IRequireState you can then type code like:

foreach(var job in myListOfJobs)
  if(job.GetType() == typeof(IRequireState))
    ((IRequireState)job).GetState();

I hope that helps you on the path to good Object Oriented design. Make sure to read up on the SOLID principles. They help alot in these questions.

If you have some jobs that do the same GetState() thing, then it would make sense to have a base class which implements the same interface, but you're then free to ALSO have jobs with various, different GetState() implementations.

Upvotes: 0

George Ionita
George Ionita

Reputation: 11

If using an interface is not absolutely required by your design, you can use an abstract class instead of the interface and not both at the same time. It will work practically the same way but allows you to define a method/property implementation.

Your case is a common pattern addressed by abstract classes.

Upvotes: 1

Alexander Derck
Alexander Derck

Reputation: 14488

In an abstract class you should only mark the methods that need to be overridden as abstract, otherwise mark them virtual:

public abstract class MyBase
{
    protected virtual void DoSomething()
    {
        //My Implementation here
        Console.WriteLine("Base implementation");
    }

    //Will give compile-time error if you don't override this in derived class
    protected abstract void DoSomethingElse();
}

Then in your derived classes you can use either the base implementation of the virtual methods or override them and the abstract methods will need to be implemented:

public class Derived : MyBase
{
    protected override void DoSomethingElse()
    {
        Console.WriteLine("Derived implementation");
    }
}

public static void Main(String[] args)
{
    var derived = new Derived();
    derived.DoSomething();      //Base Implementation
    derived.DoSomethingElse();  //Derived implementation
}

Upvotes: 3

Related Questions