The problem is that you want the logic to control the _connected flag, which should be common to all subclasses, but at the same time retain the ability of each subclass to provide its own implementation of what happens when you actually call Connect () and disconnect ( ) For this, I would recommend what is called the Template method template , so that the abstract base class can still retain some control over the logic used in the method. The way this works is pretty simple, just structure your class like this:
public void Connect() { if(!_connected) { ExecuteConnect(); _connected = true; } } public void Disconnect() { if(_connected) { ExecuteDisconnect(); _connected = false; } } protected abstract void ExecuteConnect(); protected abstract void ExecuteDisconnect();
Now in the base classes, you can override the Execute * methods (I'm sure the naming convention can be improved) so that you can provide new implementations for connecting and disconnecting, while maintaining control over the general algorithm.
source share