저는 통신 프로젝트를 진행 중입니다. 내 프로젝트에서 Open/Closed 원칙을 구현했습니다. 아래는 수업입니다. 나는 비디오 서비스를 구현해야하는 경우OOPS 열기/닫기 원칙
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;
}
}
, 나는이 만들어집니다 새로운 Video_Service 클래스. 나는 Open/Close 원칙을 달성 할 것이라고 믿습니다.
MainServiceClass.cs에 새 메서드를 추가해야하는 경우 새 메서드 (GetNewTypeOfData())를 추가합니다.
질문 : 여기서 수업을 수정하고 있습니다. 그래도 OCP를 따르고 있습니까? 또는 MainServiceClass.cs에 새 메서드를 추가하는 방법이 있습니까 ??
좋습니다.
P. 나는 모든 파생 클래스 즉 Web_Service.cs, Voice_Service.cs이 메소드를 구현해야하고, CrudaLilium 응답 후 내 질문에 업데이트 Video_Service.cs
.
이것은 제 이해입니다. 내가 틀렸다면 나를 바로 잡아주세요.
내 현재 코드 :
public interface IMainBase
{
IEnumerable<string> GetData2016();
}
public class VoiceService : IMainBase
{
public IEnumerable<string> GetData2016()
{
return Enumerable.Empty<string>();
}
}
내가 그래서 내가 다시 2018의 새로운 요구 사항을 얻을 것 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
내 코드를 업데이트 할 2017 년에 새로운 요구 사항을 얻을 것입니다. 위의 코드 당, 내가 OCP를 위반하고 있지 않다 그래서, 나는 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
에 내 코드를 업데이트합니다. 이것은 좋은 습관입니까 아니면 대안적인 방법이 있습니까?
당신은 모든 클래스에 나타날 새로운 방법과 구현을 추가하고 싶다고 말하고 있습니까? – Turbot
예, MainService 클래스에 새 메서드를 추가하고 나중에 파생 클래스에서 재정의합니다. – KiddoDeveloper
새 기능을 추가 할 때 기존 클래스를 변경해야하는 경우 OCP를 위반하게됩니다. 따라서 OCP를 따르지 않습니다. 너는 그것을 깨뜨리고있다. – Steven