简体   繁体   English

实现派生类接口方法的抽象基类

[英]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. 我想从本质上冻结IOld接口(将不赞成使用),并强迫用户使用INew接口,该接口将包含与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. 但是,INew方法Meth3表示与IOld Meth2相同的功能,但是返回类型不同,为了更好地区分它们,我认为这种设计是合适的。 My question is, is this considered good coding practise? 我的问题是,这被认为是良好的编码习惯吗?

NOTE: IOld and INew live in seperate assemblies. 注意:IOld和INew存在于单独的程序集中。 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. NewClass不能以任何方式引用IOld,因为它将作为可远程(MarshalByRef)对象公开,客户端将通过INew接口连接到该对象。 I don't want new clients referencing the IOld assembly. 我不希望新客户引用IOld程序集。

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;
    } 
}

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. 还要确保记录在案。

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. 最好将方法标记为不推荐使用,以告知开发人员他们需要更新代码,而不是通过杀死旧方法来破坏代码,这是一种更好的做法。

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);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在基类和派生类中实现接口 - Implementing an interface in a base and derived class 在使用抽象基 class 作为参数实现接口方法时访问派生的 class 属性 - Accessing derived class properties when implementing an interface method with abstract base class as parameter 可以在派生类中覆盖抽象类,而无需在基类中实现 - Can abstract class be override in derived class without implementing in base class 从基本抽象类(反射)调用派生的类方法 - Invoke derived class methods from base abstract class (reflection) 显式地将派生类标记为基类的实现接口 - Explicitly marking derived class as implementing interface of base class 基类实现基接口而派生/具体类实现扩展接口,为什么? - base class implementing base interface while derived/concrete class implementing extended interface, why? 重写基类抽象方法并将其隐藏在派生类中 - Overriding Base Class abstract methods and hiding them in derived classes 基类实现接口 - Base class implementing interface 为什么抽象类实现接口? - Why Abstract Class Implementing Interface? 为什么派生类不能使用扩展该接口的类型覆盖基类的接口类型抽象属性? - Why can't a derived class override base class' interface type abstract property with a type that extends that interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM