Importance of the Interface Segregation Principle
Explore the Interface Segregation Principle within the SOLID design framework, focusing on how to separate interfaces so classes only implement methods they actually use. This lesson uses C# examples involving text processing to show how segregating interfaces improves code clarity and flexibility while avoiding unnecessary method implementations.
We'll cover the following...
Example of the interface segregation principle
The following playground contains a console application that reads text from a file and converts it to HTML:
Note: Run the following code. The program will ask for the file to convert. Enter
sample.txtas the name of the file. When the program stops execution, press any key to return to the terminal. Now, enter thecat sample.htmlcommand to see the content after conversion.
This is the first paragraph. It has *bold text*. This is the second paragraph. It has **italic text**. This is the third paragraph. It has ~~text with strike-through~~.
We have a TextProcessor named base class on lines 6–23 in the code provided below that converts paragraphs in the input text into HTML paragraphs by applying relevant tags.
We also have a MdTextProcessor named derived ...