Ethan's Blog


  • Home

  • Archives

  • Tags

  • Search

Dependency Inversion Principle (DIP)

Posted on 2020-10-26

控制反转(IoC)

控制反转的英文翻译是 Inversion of Control,缩写为 IoC。我们先通过一个例子来看一下,什么是控制反转:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class UserServiceTest {
public static boolean doTest() {
//...
}

public static void main(String[] args) {
// 这部分逻辑可以放到框架中
if (doTest()) {
System.out.println("Test succeed.");
}
else {
System.out.println("Test failed.");
}
}
}

在上面的代码中,所有的流程都由程序员来控制。如果我们抽象出一个下面这样一个框架,我们再来看,如何利用框架来实现同样的功能。具体的代码实现如下所示:

Read more »

Interface Segregation Principle (ISP)

Posted on 2020-10-11

如何理解 ISP

接口隔离原则的英文翻译是“ Interface Segregation Principle”,缩写为 ISP。Robert Martin 在 SOLID 原则中是这样定义它的:

Clients should not be forced to depend upon interfaces that they do not use.

直译成中文的话就是:客户端不应该被强迫依赖它不需要的接口。其中的“客户端”,可以理解为接口的调用者或者使用者。

实际上,“接口”这个名词可以用在很多场合中。生活中我们可以用它来指插座接口等。在软件开发中,我们既可以把它看作一组抽象的约定,也可以具体指系统与系统之间的 API 接口,还可以特指面向对象编程语言中的接口等。

前面我提到,理解接口隔离原则的关键,就是理解其中的接口二字。在这条原则中,我们可以把“接口”理解为下面三种东西:

  • 一组 API 接口集合;
  • 单个 API 接口或函数;
  • OOP 中的接口概念;
Read more »

Liskov Substitution Principle (LSP)

Posted on 2020-09-28

如何理解 LSP

里式替换原则的英文翻译是:Liskov Substitution Principle,缩写为 LSP。这个原则最早是在 1986 年由 Barbara Liskov 提出,他是这么描述这条原则的:

If S is a subtype of T, then objects of type T may be replaced with objects of type S, without breaking the program.

在 1996 年,Robert Martin 在他的 SOLID 原则中,重新描述了这个原则,英文原话是这样的:

Functions that use pointers of references to base classes must be able to use objects of derived classes without knowing it.

我们综合两者的描述,将这条原则用中文描述出来,是这样的:子类对象能够替换程序中父类对象出现的任何地方,并且保证原来程序的逻辑行为(behavior)不变及正确性不被破坏。

这么说还是比较抽象,我们通过一个例子来解释一下。如下代码中,父类 Transporter 使用 org.apache.http 库中的 HttpClient 类来传输网络数据。子类 SecurityTransporter 继承父类 Transporter,增加了额外的功能,支持传输 appId 和 appToken 安全认证信息:

Read more »

Open Closed Principle (OCP)

Posted on 2020-09-18

如何理解 OCP

开闭原则的英文全称是 Open Closed Principle,简写为 OCP。它的英文描述是:

Software entities (modules, classes, functions, etc.) should be open for extension, but closed for modification.

我们把它翻译成中文就是:软件实体(模块、类、方法等)应该“对扩展开放、对修改关闭”。

这个描述比较简略,如果我们详细表述一下,那就是,添加一个新的功能应该是,在已有代码基础上扩展代码(新增模块、类、方法等),而非修改已有代码(修改模块、类、方法等)。

这是一段 API 接口监控告警的代码。其中,AlertRule 存储告警规则,可以自由设置。Notification 是告警通知类,支持邮件、短信、微信、手机等多种通知渠道。NotificationEmergencyLevel 表示通知的紧急程度,包括 SEVERE(严重)、URGENCY(紧急)、NORMAL(普通)、TRIVIAL(无关紧要),不同的紧急程度对应不同的发送渠道:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Alert 
{
private AlertRule rule;
private Notification notification;

public Alert(AlertRule rule, Notification notification)
{
this.rule = rule;
this.notification = notification;
}

public void check(String api, long requestCount, long errorCount, long durationOfSeconds)
{
long tps = requestCount / durationOfSeconds;
if (tps > rule.getMatchedRule(api).getMaxTps())
{
notification.notify(NotificationEmergencyLevel.URGENCY, "...");
}
if (errorCount > rule.getMatchedRule(api).getMaxErrorCount())
{
notification.notify(NotificationEmergencyLevel.SEVERE, "...");
}
}
}
Read more »

Single Responsibility Principle (SRP)

Posted on 2020-09-17

如何理解 SRP

单一职责原则的英文是 Single Responsibility Principle,缩写为 SRP。这个原则的英文描述是这样的:

A class or module should have a single responsibility.

如果我们把它翻译成中文,那就是:一个类或者模块只负责完成一个职责(或者功能)。

注意,这个原则描述的对象包含两个,一个是类(class),一个是模块(module)。关于这两个概念,在专栏中,有两种理解方式。一种理解是:把模块看作比类更加抽象的概念,类也可以看作模块。另一种理解是:把模块看作比类更加粗粒度的代码块,模块中包含多个类,多个类组成一个模块。

单一职责原则的定义描述非常简单,也不难理解。一个类只负责完成一个职责或者功能。也就是说,不要设计大而全的类,要设计粒度小、功能单一的类。换个角度来讲就是,一个类包含了两个或者两个以上业务不相干的功能,那我们就说它职责不够单一,应该将它拆分成多个功能更加单一、粒度更细的类。

如何判断 SRP

在一个社交产品中,我们用下面的 UserInfo 类来记录用户的信息。你觉得,UserInfo 类的设计是否满足单一职责原则呢:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class UserInfo 
{
private long userId;
private String username;
private String email;
private String telephone;
private long createTime;
private long lastLoginTime;
private String avatarUrl;
private String provinceOfAddress; // 省
private String cityOfAddress; // 市
private String regionOfAddress; // 区
private String detailedAddress; // 详细地址
//...省略其他属性和方法...
}
Read more »
1…383940…54
necusjz

necusjz

270 posts
16 tags
© 2016 - 2026 necusjz
Powered by Hexo
Theme - NexT.Mist