开发者

C# Abstract function with implementation possible?

开发者 https://www.devze.com 2023-03-22 07:51 出处:网络
Is there a way to add a virtual function that must be overridden by all inherited classes? So actually the combination of virtual and abstract?

Is there a way to add a virtual function that must be overridden by all inherited classes? So actually the combination of virtual and abstract? I have a situation where each inherited class must do some specific processing before some generic code is executed. Virtual functions doesn't work be开发者_运维知识库cause they do not ensure the inherited classes override them. And abstract function can't have a default implementation. Currently my workaround is to implement another protected function in the base class which contains the common/generic code and is called in the overridden abstract function


It is not possible to have a method that is both abstract and virtual.

If possible, you can split your method in a "before" and "after" part:

public void DoWork()
{
    DoBeforeWork();
    DoCommonWork();
    DoAfterWork();
}

protected abstract void DoBeforeWork();
protected abstract void DoAfterWork();

private void DoCommonWork() { ... }

Otherwise, your workaround with a second protected method is a very good idea:

public void DoWork()
{
    DoActualWork();
}

protected abstract void DoActualWork(); // expected to call DoCommonWork

protected void DoCommonWork() { ... }

You can check if DoCommonWork was really called in DoWork using a thread-local field if necessary.

However, I'd probably go with making the method virtual. If the derived class doesn't want to add anything to the common part, it shouldn't have to:

public virtual void DoWork() { ... }

Again, you can check if the common part was really called.


I think you might be looking for a template method: Implement a method in your base class that calls an abstract method as part of its implementation. Derived classes then can implement the abstract method, but this allows the base class to do "some specific processing" before this method is executed.


My suggestion is to split the base class into two: classes one abstract and one concrete sealed.

Example

I assume that you have base class which looks like that:

public class BaseClass
{
    public void DoProcessing()
    {
        // this method must be overriden in each subclass
    }
}

Let's split it to two:

public abstract class BaseClass
{
    protected abstract DoProcessingImlementation();    

    public void DoProcessing()
    {
        // here you can add program logic or just call overridden implementation
        DoProcessingImplementation();
    }
}

public sealed class DefaultBaseClassImplementation : BaseClass
{
    public override DoProcessingImlementation()
    {
        // default implementation of method
    }
}

With this design if you need to implement new processing logic, you inherit new class from the BaseClass and because it's abstract you must implement the DoSomethigImplementation method. To access the default processing logic you should use an instance of the DefaultBaseClassImplementation class. And that the DefaultBaseClassImplementation is sealed will not let you inherit from the wrong class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号