Interface, Abstract, or just virtual methods?

狂风中的少年 提交于 2019-12-05 03:34:29

Your abstract and 'super base class' approaches are not too different. You should always make the base class abstract, and you can provide a default implementation (virtual methods) or not (abstract methods). The deciding factor is whether you ever want to have instances of the base class, I think not.

So it's between base class and interface. If there is a strong coupling between your A, B C classes then you can use a base class and probably a common implementation.

If the A, B, C classes do not naturally belong to a single 'family' then use an interface.

And System is not such a good name.

And you cannot change the parameterlist when overriding. Maybe default parameters can help, otherwise you just need 2 overloads for GetPropertyInformation().

Generally you choose object inheritance when you want to share implementation and reduce what would otherwise be duplication. Otherwise interfaces win because they are more flexible since there is no need for a common base class.

As for overriding a method and modifying the parameter list that's just not possible. Imagine how you would call that method on a base class or an interface reference?

I'd go with something like what I've added below. You still get the benefit of the interface contract and shared implementation.

public Interface ISystem
{
    public void GetPropertyInformation();
    //Other methods to implement
}

public abstract class System : ISystem
{
    public virtual void GetPropertyInformation()
    {
        //Standard Code here
    }
}

public class B : System
{  
   public string ExtendedSystemProp {get;set;}

   public override void GetPropertyInformation()
   {
      base.GetPropertyInformation();

      var prop = "some extra calculating";

      GetExtraPropertyInformation(prop);
    }

    public void GetExtraPropertyInformation(string prop)
    {
         ExtendedSystemProp = prop;
    }
}

ISystem genericSystem = new B();
genericSystem.GetPropertyInformation();

(genericSystem as B).ExtendedSystemProp = "value";

You cannot pass the extra parameter in the override. When you are overriding, you are overriding the method with the exact signature. I would suggest you pass in an interface parameter like IPropertyInformation that can change per implementation.

The decision to go with a base class or an interface for your implementation really depends upon your use. Do A-I have enough in common with each other that they should really all derive from the same base class? If so, then use a base class. Is it really that just GetPropertyInformation is shared and otherwise the systems are totally functionally different? Then you really just want them to share an interface.

Others have covered what was originally in my answer, but about the "adding a parameter" point: Don't forget that the latest C# also lets you have optional parameters in methods.

Unless you have a compelling reason otherwise, I'd go with the interface. Public virtual methods, though done a lot, is not ideal.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!