Introduction
Interface-segregation principle
ISP states that no client should be forced to depend on methods it does not use.ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them. Such shrunken interfaces are also called role interfaces. Having a client that is forced to use unnecessary method is regarded as violated the interface-segregation principle
Assumptions
Assuming we had completed the Liskov example in our SOLID Principle. We should now realized that the requirement stated that only messages that is sent external need to be protected. In our Liskov example, we somehow had a redundant method in InternalMessage class. This is the perfect time to make use of interface-segregation principle.
The Interface segregation
We may then introduce a separate interface for the protected message like below
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace LiskovHarryKanePenalty.IMicroMessage | |
{ | |
public interface IProtectedMessage | |
{ | |
void ProtectData(); | |
} | |
} |
Update The ExternalMessage Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace LiskovHarryKanePenalty.IMicroMessage | |
{ | |
public class ExternalMessageChannel : IMicromessage , IProtectedMessage | |
{ | |
private string _Message; | |
public string SendData() | |
{ | |
return _Message; | |
} | |
public void ProtectData() | |
{ | |
this._Message = Encryption.ProtectMicroservices(this._Message); | |
} | |
} | |
} |
The Implementation
We next may perform similar implementation without any changes like below
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace LiskovHarryKanePenalty.IMicroMessage | |
{ | |
public static class MicroHttpService | |
{ | |
public static string ClassSendData(IMicromessage Data) | |
{ | |
if(Data is ExternalMessageChannel) | |
((ExternalMessageChannel)Data).ProtectData(); | |
return Data.SendData(); | |
} | |
} | |
} |
Conclusion
From the article, we may see that we are following the SOLID principle by removing unnecessary method by using the Interface-segregation principle.
Ref no : DTC-WPUB-000104
About Author

Comments
Post a Comment