Best practice using abstract / virtual

Firt of all, sorry for my "EngRish". I'm revisiting some of my project to make them more usable/understandable by my co-workers; I'm developing a framework for integrate a protocol driver to our SCADA system, in a nutshell i have an abstract class that provide some common functionalities that must specialized by the protocol driver, for example:

Opening a comunication with the field:

Called Method: void ConnectionOnScan(string connectionName)

In the base class i have something like this:

public void ConnectionOnScan(string connectionName)
{
  //... some preliminary operation here

  try
  {
    ConnectionOnScan(channelNumber);//provided by the derived class
  }
  catch (Exception e)
  {
   //some code here to properly register the fault to the SCADA System
  }
 }

The method that should be provided by the derived class should be (according to the system):

void ConnectionOnScan(UInt64 channelNumber)

Normally the driver must do something inside this method, in small cases this method can be empty.

My Goal is to let the develompent of a driver as simple as possible even for low-skilled programmers.

I have around 30 methods with the same use case: what will be best practice? declare them abstract or virtual (with an empty method)?


You should ask yourself if you have to implement the method in the driver, or if it is optional.

In the first case you use abstract, in the second case you use virtual.

链接地址: http://www.djcxy.com/p/38136.html

上一篇: 何时使用接口而不是抽象类,反之亦然?

下一篇: 使用抽象/虚拟的最佳实践