Skip to content

Commit c7d79f2

Browse files
author
angelo.yanve
committed
feat: add Interface Segragation principle implementation
1 parent d8a8dea commit c7d79f2

File tree

4 files changed

+115
-5
lines changed

4 files changed

+115
-5
lines changed

04-SOLID/04x04_ISP/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Example
2+
3+
### Description
4+
Here we are going to take an example on a `AudioPlayer`, `VideoPlayer` and `MediaPlayer` interface.
5+
6+
### Bad point of view
7+
In the bad ISP example, we have a `MediaPlayer` interface that has methods for both audio and video. This is a violation of the ISP because the `AudioPlayer` and `VideoPlayer` classes are forced to implement methods that they don't need.
8+
9+
### Good point of view
10+
In the good ISP example, we have a `AudioPlayer` interface and a `VideoPlayer` interface. This is a better design because the `AudioPlayer` and `VideoPlayer` classes are only forced to implement the methods that they need.
11+
12+
Then We can see a use cases example

04-SOLID/04x04_ISP/bad.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
interface MediaPlayer{
3+
playAudio(): void;
4+
playVideo(): void;
5+
showLyrics(): void;
6+
showSubtitles(): void;
7+
}
8+
9+
class _MusicApp implements MediaPlayer {
10+
playAudio() {
11+
console.log('Playing audio');
12+
}
13+
playVideo() {
14+
console.log('Playing video');
15+
// This method is not needed for MusicApp
16+
//! Here we are breaking the Interface Segregation Principle
17+
}
18+
showLyrics() {
19+
console.log('Showing lyrics');
20+
}
21+
showSubtitles() {
22+
console.log('Showing subtitles');
23+
// This method is not needed for MusicApp
24+
//! Here we are breaking the Interface Segregation Principle
25+
}
26+
}
27+
28+
class _VideoPlayer implements MediaPlayer {
29+
playAudio() {
30+
console.log('Playing audio');
31+
// This method is not needed for VideoPlayer
32+
//! Here we are breaking the Interface Segregation Principle
33+
}
34+
playVideo() {
35+
console.log('Playing video');
36+
}
37+
showLyrics() {
38+
console.log('Showing lyrics');
39+
// This method is not needed for VideoPlayer
40+
//! Here we are breaking the Interface Segregation Principle
41+
}
42+
showSubtitles() {
43+
console.log('Showing subtitles');
44+
}
45+
}
46+

04-SOLID/04x04_ISP/good.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
interface AudioPlayer {
2+
playAudio(): void;
3+
showLyrics(): void;
4+
}
5+
6+
interface VideoPlayer {
7+
playVideo(): void;
8+
showSubtitles(): void;
9+
}
10+
11+
class MusicApp implements AudioPlayer {
12+
playAudio() {
13+
console.log('Playing audio');
14+
}
15+
showLyrics() {
16+
console.log('Showing lyrics');
17+
}
18+
}
19+
20+
class VideoPlayer implements VideoPlayer {
21+
playVideo() {
22+
console.log('Playing video');
23+
}
24+
showSubtitles() {
25+
console.log('Showing subtitles');
26+
}
27+
}
28+
29+
// This way we are following the Interface Segregation Principle
30+
// We are not forcing the clients to implement methods they don't need
31+
32+
//? This is just an example to know that we can implement multiple interfaces in a class
33+
class MusicVideoApp implements AudioPlayer, VideoPlayer {
34+
playAudio() {
35+
console.log('Playing audio');
36+
}
37+
showLyrics() {
38+
console.log('Showing lyrics');
39+
}
40+
playVideo() {
41+
console.log('Playing video');
42+
}
43+
showSubtitles() {
44+
console.log('Showing subtitles');
45+
}
46+
}

04-SOLID/README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,29 @@ Principle SOLID 5 software design principles that help to create a good software
1212
- Interface Segregation Principle
1313
- Dependency Inversion Principle
1414

15-
### Single Responsibility Principle (SRP)
15+
## Single Responsibility Principle (SRP)
1616
***A class should have only one reason to change*** \
1717
We determine responsability of one class according to the **social structure** of the **users usage**. \
1818
**Example:** go to file [SingleResponsibilityPrinciple](./04x01_SRP)
1919

20-
### Open-Closed Principle (OCP)
20+
## Open-Closed Principle (OCP)
2121
***A class should be open for extension, but closed for modification*** \
2222
This principle is about **extending** the behavior of a class without **modifying** it. So to add new functionality, it shouldn't require changing the existing code.
2323

2424
**Tips**: To do this, we write **interfaces** and **abstract classes** in order to dictate the higher-level policy that needs to be implemented, and then we implement that policy using concrete classes.
2525

2626
**Example:** go to file [OpenClosedPrinciple](./04x02_OCP)
2727

28-
### LisKov-Substitution Principle (LSP)
28+
## LisKov-Substitution Principle (LSP)
2929
***Derived classes must be substitutable for their base classes***
3030

31-
3231
This principle is about **inheritance** and **polymorphism**. It states that if a program is using a base class, then the reference to the base class can be replaced with a derived class without affecting the functionality of the program.
3332

34-
**Example:** go to file [LiskovSubstitutionPrinciple](./04x03_LSP)
33+
**Example:** go to file [LiskovSubstitutionPrinciple](./04x03_LSP)
34+
35+
## Interface Segregation Principle (ISP)
36+
***A client should never be forced to implement an interface that it doesn't use***
37+
38+
This principle is about **interfaces**. It states it’s better to have `smaller`, `focused` **interfaces** rather than large, monolithic ones.
39+
40+
**Example:** go to files[InterfaceSegregationPrinciple](./04x04_ISP)

0 commit comments

Comments
 (0)