Jeb
Jeb

Reputation: 3789

Abstract base class implementing derived class interface methods

I have the following classes and interfaces. I want to essentially freeze the IOld interface (which will be deprecated) and force users to use the INew interface which will contain the same methods as IOld. However INew method Meth3 represents the same functionality as IOld Meth2 but with a differing return type, and to distinguish them better I thought this design would be suitable. My question is, is this considered good coding practise?

NOTE: IOld and INew live in seperate assemblies. NewClass cannot reference IOld in any way since it will be exposed as a remoteable (MarshalByRef) object which clients will connect to via the INew interface. I don't want new clients referencing the IOld assembly.

public interface IOld
{
    void Meth1();
    double Meth2(int input);
} 

public interface INew
{
    void Meth1();
    float Meth3(int input);
} 

public abstract class BaseClass
{
    public virtual void Meth1()
    {
    } 

    public virtual double Meth2(int input)
    {
        throw new NotImplementedException();
    } 
} 

public class OldClass
:
    BaseClass,
    IOld
{
    public override double Meth2(int input)
    {
        return 0;
    } 
} 

public class NewClass
:
    BaseClass,
    INew
{
    public float Meth3(int input)
    {
        return 0;
    } 
}

Upvotes: 1

Views: 1155

Answers (3)

3Dave
3Dave

Reputation: 29041

I'd try something like this:

public class OldClass : BaseClass, IOld
{
    public override double Meth2(int input)
    {
        return 0.0;
    }
}

public class NewClass : OldClass, INew // now implements IOld and INew using the Method2() from OldClass
{
    [Obsolete("This method is deprecated . Use Method3() instead.")]
    public override double Meth2(int input)
    {
        return base.Meth2(input);
    }
    public float Meth3(int input)
    {
        return return 0.0f;
    }
}

It's considered better practice to flag a method as deprecated to inform the developer that they need to update their code than to break it by killing the old method.

Upvotes: 2

brgerner
brgerner

Reputation: 4371

I would prefer to add a new method with little bit different name instead of creating new interface.

So the old interface would be for example:

public interface IOld
{
    void Meth1();
    [Obsolete("Use Meth22 instead.")]
    double Meth2(int input);
    float Meth22(int input);
} 

Upvotes: 2

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

Sit back down and build your pyramid with a solid base. The way you have set things up, the abstract base class and interface are competing methods of accomplishing the same concept. You are doing this primarily to facilitate Old versus New, but you end up with a rather funky dependency graph when you do this.

With what you have written, your only gotcha is methods with the same signature. If it were not for this issue, you could easily extend the old interface and not end up with issues. If that method changes in the new, is it a refactor change (acceptable) or a completely different result? In other words, can the old client call the new? If so, there is no need to have that method on a different interface either.

Make sure you deprecate the methods, so users are aware they are going to lose the old methods. Also make sure it is documented.

Upvotes: 3

Related Questions