SOLID原则是一套面向对象编程的设计原则,旨在创建更健壮、易维护和可扩展的代码。本文将通过TypeScript示例,简要介绍SOLID原则中的前三个原则:单一职责原则(SRP)、开闭原则(OCP)和里氏替换原则(LSP)。
S - 单一职责原则 (SRP)
一个类应该只有一个变化的原因。 这意味着每个类应该只负责一个特定的功能。如果一个类承担了过多的职责,就应该将其分解成更小的、更专注的类。
反例:
1
2
3
4
5
6
7
8
9
class UserService {
createUser(user: string): void {
console.log(`User ${user} created`);
}
sendWelcomeEmail(user: string): void {
console.log(`Welcome email sent to ${user}`);
}
}
UserService类同时负责创建用户和发送邮件,违反了SRP原则。
改进后的例子:
1
2
3
4
5
6
7
8
9
10
11
12
class UserService {
createUser(user: string): string {
console.log(`User ${user} created`);
return user;
}
}
class EmailService {
sendEmail(user: string): void {
console.log(`Email sent to ${user}`);
}
}
职责被清晰地划分到UserService和EmailService两个类中。
如何有效应用SRP? 将职责基于业务逻辑进行分组。例如,与邮件相关的操作属于EmailService,与用户相关的操作属于UserService。
O - 开闭原则 (OCP)
软件实体应该对扩展开放,对修改关闭。 这意味着可以向系统添加新功能,而无需修改现有代码。
反例:
1
2
3
4
5
6
7
8
9
10
class DiscountService {
calculateDiscount(discountType: string, amount: number): number {
if (discountType === standard) {
return amount * 0.1;
} else if (discountType === premium) {
return amount * 0.5;
}
return 0;
}
}
改进后的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
interface Discount {
calculateDiscount(amount: number): number;
}
class StandardDiscount implements Discount {
calculateDiscount(amount: number): number {
return amount * 0.1;
}
}
class PremiumDiscount implements Discount {
calculateDiscount(amount: number): number {
return amount * 0.5;
}
}
class DiscountService {
constructor(private discount: Discount) {}
calculateDiscount(amount: number): number {
return this.discount.calculateDiscount(amount);
}
}
const standardDiscountService = new DiscountService(new StandardDiscount());
const premiumDiscountService = new DiscountService(new PremiumDiscount());
通过接口和多态性,可以轻松添加新的折扣类型,而无需修改DiscountService类。
L - 里氏替换原则 (LSP)
子类型必须能够替换其基类型。 这意味着子类应该能够在不改变程序正确性的前提下替换其父类。
反例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Rectangle {
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
getArea(): number {
return this.width * this.height;
}
}
class Square extends Rectangle {
constructor(side: number) {
super(side, side);
}
set width(width: number) {
this.width = this.height = width;
}
set height(height: number) {
this.height = this.width = height;
}
}
Square类违反了LSP原则,因为它改变了Rectangle类的预期行为。
改进后的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
abstract class Shape {
abstract getArea(): number;
}
class Rectangle extends Shape {
constructor(protected width: number, protected height: number) {
super();
}
getArea(): number {
return this.width * this.height;
}
}
class Square extends Shape {
constructor(protected side: number) {
super();
}
getArea(): number {
return this.side * this.side;
}
}
Square和Rectangle都实现了Shape接口,且各自保持了独立的实现逻辑,避免了行为冲突。
如何有效应用LSP? 当子类与父类的行为不完全一致时,避免强制继承,考虑使用组合或接口。
(待续…)
以上就是【A部分SOLID Typescript简介的详细内容,更多请关注php中文网其它相关文章!