Skip to content

Commit d8a8dea

Browse files
author
angelo.yanve
committed
feat: add Liskov substitution principle implementation
1 parent cf44a30 commit d8a8dea

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

04-SOLID/04x02_OCP/good.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ interface IEmailService {
1010
sendMail(mail: Mail): Promise<EmailTransmissionResult>;
1111
}
1212

13-
class Mail {
14-
// Define properties and methods for the Mail class
13+
type Mail = {
14+
// Define properties and methods for the Mail interface
1515
subject: string;
1616
from: string;
1717
to: string[];

04-SOLID/04x03_LSP/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Example
2+
Here we are going to create a simple example to illustrate the Liskov Substitution Principle.
3+
4+
We will use the same example of the Open-Closed Principle, with `IEmailService` and it's implementations `MailGunEmailService`, `SendGridEmailService`, `MailChimpEmailService`...

04-SOLID/04x03_LSP/good.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
type TransmissionResult = 'Success' | 'Failure' | 'Bounced'
2+
3+
interface IEmailTransmissionResult {
4+
result: TransmissionResult;
5+
message?: string;
6+
}
7+
8+
type Mail = {
9+
// Define properties and methods for the IMail interface
10+
subject: string;
11+
from: string;
12+
to: string[];
13+
body: any;
14+
}
15+
16+
interface IMailService {
17+
sendMail(email: Mail): Promise<IEmailTransmissionResult>
18+
}
19+
20+
class SendGridEmailService implements IMailService {
21+
sendMail(email: Mail): Promise<IEmailTransmissionResult> {
22+
// algorithm
23+
return Promise.resolve({ result: 'Success' });
24+
}
25+
}
26+
27+
class MailChimpEmailService implements IMailService {
28+
sendMail(email: Mail): Promise<IEmailTransmissionResult> {
29+
// algorithm
30+
return Promise.resolve({ result: 'Success' });
31+
32+
}
33+
}
34+
35+
class MailGunEmailService implements IMailService {
36+
sendMail(email: Mail): Promise<IEmailTransmissionResult> {
37+
// algorithm
38+
return Promise.resolve({ result: 'Success' });
39+
40+
}
41+
}
42+
// ...
43+
44+
45+
46+
// Go to use-case.ts to see the why the LSP is respected

04-SOLID/04x03_LSP/use-case.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
let emailService : IMailService = new SendGridEmailService();
3+
4+
const mailGunEmailService = new MailGunEmailService();
5+
6+
emailService = mailGunEmailService;
7+
8+
// This is valid because MailGunEmailService is a subtype of IMailService

04-SOLID/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ This principle is about **extending** the behavior of a class without **modifyin
2626
**Example:** go to file [OpenClosedPrinciple](./04x02_OCP)
2727

2828
### LisKov-Substitution Principle (LSP)
29-
***Derived classes must be substitutable for their base classes*** \
29+
***Derived classes must be substitutable for their base classes***
30+
31+
3032
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.
3133

34+
**Example:** go to file [LiskovSubstitutionPrinciple](./04x03_LSP)

0 commit comments

Comments
 (0)