I am working on a telecom project. I have implemented Open/Closed principle in my project. Below are my classes.
MainServiceClass.CS
public abstract class BaseServiceClass
{
public abstract IEnumerable<string> GetServiceData();
public abstract IEnumerable<string> GetDashBoardData();
}
Web_Service.CS
public class WebServiceClass : BaseServiceClass
{
public override IEnumerable<string> GetServiceData()
{
List<string> MyList = new List<string>();
return MyList;
}
public override IEnumerable<string> GetDashBoardData()
{
List<string> MyList = new List<string>();
return MyList;
}
}
Voice_Service.CS
public class VoiceSericeClass : BaseServiceClass
{
public override IEnumerable<string> GetServiceData()
{
List<string> MyList = new List<string>();
return MyList;
}
public override IEnumerable<string> GetDashBoardData()
{
List<string> MyList = new List<string>();
return MyList;
}
}
In the future, If I need to implement Video service, I will create a new Video_Service class.I believe that I will achieve Open/Close principle.
If I need to add a new method in my MainServiceClass.cs , I will add a new Method (GetNewTypeOfData()).
Question : Here, I am modifying a class. Still, am I following OCP? Or, is there any way to add new method in the MainServiceClass.cs??
Please suggest.
P.S. I need to implement this method in all the derived classes i.e. Web_Service.cs, Voice_Service.cs, and Video_Service.cs
Updating my question after CrudaLilium reply.
This is my understanding. Please correct me if I am wrong.
My Current Code :
public interface IMainBase
{
IEnumerable<string> GetData2016();
}
public class VoiceService : IMainBase
{
public IEnumerable<string> GetData2016()
{
return Enumerable.Empty<string>();
}
}
I would get a new requirement in 2017. So, I will update my code in 2017
public interface IMainBase
{
IEnumerable<string> GetData2016();
}
public class VoiceService : IMainBase
{
public IEnumerable<string> GetData2016()
{
return Enumerable.Empty<string>();
}
}
//New Code will be Added in 2017....Start
public interface IMainBase2017 : IMainBase
{
IEnumerable<string> GetData2017();
}
public class voiceService2017 : VoiceService, IMainBase2017
{
public IEnumerable<string> GetData2017()
{
return Enumerable.Empty<string>();
}
}
//New Code Added be in 2017...Ended
I would again get a new requirement in 2018. So, I will update my code in 2018.
public interface IMainBase
{
IEnumerable<string> GetData2016();
}
public class VoiceService : IMainBase
{
public IEnumerable<string> GetData2016()
{
return Enumerable.Empty<string>();
}
}
//New Code will be Added in 2017....Start
public interface IMainBase2017 : IMainBase
{
IEnumerable<string> GetData2017();
}
public class voiceService2017 : VoiceService, IMainBase2017
{
public IEnumerable<string> GetData2017()
{
return Enumerable.Empty<string>();
}
}
//New Code Added be in 2017...Ended
//New Code will be Added in 2018...Start
public class WebService2018 : IMainBase2017
{
public IEnumerable<string> GetData2016()
{
return Enumerable.Empty<string>();
}
public IEnumerable<string> GetData2017()
{
return Enumerable.Empty<string>();
}
}
//New Code will be Added in 2018...End
As per above code, I am not violating OCP. Is this a good practice or do I have any alternative way also?