I've been working with some abstract classes and something feels off but I'm not sure what to do about it. Here's a simple example to explain my question:
public abstract class Data
{
protected Boolean _Connected = false;
public abstract void Connect();
public abstract void Disconnect();
public Boolean Connected
{
get { return _Connected; }
}
}
public class SqlServerData : Data
{
public override void Connect()
{
// code to connect to sql server
_Connected = true;
}
public override void Disconnect()
{
if (_Connected)
{
// code to disconnect from sql server
_Connected = false;
}
}
}
I have a couple problems with the design.
Nothing in this design forces a new implementation of Data to do anything with the _connected instance variable. If other classes using the Data class rely on the Connected property, it may not be accurate.
It can be confusing to someone new to this code. This sample is pretty simple but if there are more instance variables in the abstract class and other instance variables in the SqlServerData class it will be more difficult to know where the variables are defined.
So my question is, should this be refactored and if so, how?