Reputation: 46651
Assume I have some program that pulls airline data. Each airline data uses a different system in which I need to pull the data (xml) from. Because of the difference in each system, basically the xml being different and maybe some propeterties, I decide to implement an interface to implement for each airline system. What I find though is that each airline, although different in many aspects, also contains many similarities, so if I do it the interface way first, I might have to do something like this:
public interface IAirlineSystem {
public void PullData();
}
Now for each airline system (I am making these names up), I might do something like:
public class Oberon : IAirlineSystem {
string Name {get;set;}
int Code {get;set;}
public void Initialize()
{
//initialize code here
}
public void PullData()
{
//code here
}
}
The above is just one airline system, but imagine I have more (like +10), another one might be:
public class AirSys: IAirlineSystem {
string Name {get;set;}
int Code {get;set;}
public void Initialize()
{
//initialize code here
}
public void PullData()
{
//code here
}
}
In the code above:
The name and code properties are unique to each Airline system. Initialize contains the exact same code for each implementation and PullData is unique to each system. Because of duplicates in the classes, is this where I could use an abstract class to hold the code of the Initialize method. I have heard that it is good practice to actually mix interfaces and abstract classes, what would be an example of this using my airline system example?
Another thing that comes up is that lets assume that I have another method in the interface called ValidateData, but not all airline systems need to call it, but if I put it in an interface, I would need to implement an empty method if it is not needed. Is there a way I can prevent from having to do this. Is this another reason for using an abstract class, maybe with a virtual method, so it can be overridden as needed. If I made ValidateData an abstract method, it would still need to be implemented, right?
Upvotes: 0
Views: 144
Reputation: 578
Your explanation seems to point towards using an abstract class. You could have an abstract class that would have an implementation of the Initialize method, and you would not need to repeat it in the Oberon and AirSys subclasses. PullData could be an abstract method that would then get implemented in the separate subclasses. And yes, only the class that requires the ValidateData method would have it implemented.
Example would be:
public abstract class AirlineSystemBase
{
string Name { get; set; }
int Code { get; set; }
public void Initialize()
{
//code goes here
}
public abstract void PullData();
}
public class Oberon : AirlineSystemBase
{
public override void PullData()
{
//code goes here
}
}
public class AirSys : AirlineSystemBase
{
/// <summary>
/// that is if say only AirSys class had a need for this ValidateData() method
/// </summary>
public void ValidateData()
{
//code goes here
}
public override void PullData()
{
//code goes here
}
}
OR if you want to use both interface and abstract class:
public interface IAirlineSystem
{
void PullData();
void ValidateData();
}
public abstract class AirlineSystemBase : IAirlineSystem
{
string Name { get; set; }
int Code { get; set; }
public void Initialize()
{
//code goes here
}
public abstract void PullData();
public virtual void ValidateData()
{
//code goes here
}
}
public class Oberon : AirlineSystemBase
{
public override void PullData()
{
//code goes here
}
}
public class AirSys : AirlineSystemBase
{
/// <summary>
/// that is if say only AirSys class had a need for this ValidateData() method
/// </summary>
public override void ValidateData()
{
//code goes here
}
public override void PullData()
{
//code goes here
}
}
Upvotes: 1
Reputation: 2372
An example, I would implement something like:
public interface IAirlineSystem {
public void PullData();
public void ValidateData();
}
public abstract class BaseAirlineSystem : IAirlineSystem {
string Name {get;set;}
int Code {get;set;}
public void Initialize()
{
// common initialize code here
}
public virtual void ValidateData()
{
//empty, override when necessary
}
}
And a sample implementation:
public class AirSys: BaseAirlineSystem{
public void PullData()
{
//code here
}
}
Upvotes: 0