《架构整洁之道》:Chap11. DIP: THE DEPENDENCY INVERSION PRINCIPLE DIP:依赖反转原则

The Dependency Inversion Principle (DIP) tells us that the most flexible systems are those in which source code dependencies refer only to abstractions, not to concretions. 依赖反转原则(DIP)主要想告诉我们的是,如果想要设计一个灵活的系统,在源代码层次的依赖关系中就应该多引用抽象类型,而用具体实现。 In a statically typed language, like Java, this means that the use, import, and include statements should refer only to source modules containing interfaces, abstract classes, or some other kind of abstract declaration. Nothing concrete should be depended on. 也就是说,在 Java 这类静态类型的编程语言中,在使用 use、import、include 这些语句时应该只引用那些包含接口、抽象类或者其他抽象类型声明的源文件,不应该引用任何具体实现。 ...

March 14, 2024

《架构整洁之道》:Chap10. ISP: THE INTERFACE SEGREGATION PRINCIPLE ISP:接口隔离原则

The Interface Segregation Principle (ISP) derives its name from the diagram shown in Figure 10.1. “接口隔离原则(ISP)”这个名字来自图 10.1 所示的这种软件结构。 The Interface Segregation Principle In the situation illustrated in Figure 10.1, there are several users who use the operations of the OPS class. Let’s assume that User1 uses only op1, User2 uses only op2, and User3 uses only op3. 在图 10.1 所描绘的应用中,有多个用户需要操作 OPS 类。现在,我们假设这里的 User1 只需要使用 op1,User2 只需要使用 op2,User3 只需要使用 op3。 Now imagine that OPS is a class written in a language like Java. Clearly, in that case, the source code of User1 will inadvertently depend on op2 and op3, even though it doesn’t call them. This dependence means that a change to the source code of op2 in OPS will force User1 to be recompiled and redeployed, even though nothing that it cared about has actually changed. ...

March 13, 2024

《架构整洁之道》:Chap9. LSP: THE LISKOV SUBSTITUTION PRINCIPLE LSP:里氏替换原则

In 1988, Barbara Liskov wrote the following as a way of defining subtypes. 1988 年,Barbara Liskov 在描述如何定义子类型时写下了这样一段话: What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.1 ...

March 12, 2024

《架构整洁之道》:Chap8. OCP: THE OPEN-CLOSED PRINCIPLE OCP:开闭原则

The Open-Closed Principle (OCP) was coined in 1988 by Bertrand Meyer.1 It says: 开闭原则(OCP)是 Bertrand Meyer 在 1988 年提出的,该设计原则认为: A software artifact should be open for extension but closed for modification. 设计良好的计算机软件应该易于扩展,同时抗拒修改。 In other words, the behavior of a software artifact ought to be extendible, without having to modify that artifact. 换句话说,一个设计良好的计算机系统应该在不需要修改的前提下就可以轻易被扩展。 This, of course, is the most fundamental reason that we study software architecture. Clearly, if simple extensions to the requirements force massive changes to the software, then the architects of that software system have engaged in a spectacular failure. ...

March 11, 2024

《架构整洁之道》:Chap7. SRP: THE SINGLE RESPONSIBILITY PRINCIPLE SRP:单一职责原则

Of all the SOLID principles, the Single Responsibility Principle (SRP) might be the least well understood. That’s likely because it has a particularly inappropriate name. It is too easy for programmers to hear the name and then assume that it means that every module should do just one thing. SRP 是 SOLID 五大设计原则中最容易被误解的一个。也许是名字的原因,很多程序员根据 SRP 这个名字想当然地认为这个原则就是指:每个模块都应该只做一件事。 Make no mistake, there is a principle like that. A function should do one, and only one, thing. We use that principle when we are refactoring large functions into smaller functions; we use it at the lowest levels. But it is not one of the SOLID principles—it is not the SRP. ...

March 10, 2024

《架构整洁之道》:Part3. DESIGN PRINCIPLES 设计原则

Good software systems begin with clean code. On the one hand, if the bricks aren’t well made, the architecture of the building doesn’t matter much. On the other hand, you can make a substantial mess with well-made bricks. This is where the SOLID principles come in. 通常来说,要想构建一个好的软件系统,应该从写整洁的代码开始做起。毕竟,如果建筑所使用的砖头质量不佳,那么架构所能起到的作用也会很有限。反之亦然,如果建筑的架构设计不佳,那么其所用的砖头质量再好也没有用。这就是 SOLID 设计原则所要解决的问题。 The SOLID principles tell us how to arrange our functions and data structures into classes, and how those classes should be interconnected. The use of the word “class” does not imply that these principles are applicable only to object-oriented software. A class is simply a coupled grouping of functions and data. Every software system has such groupings, whether they are called classes or not. The SOLID principles apply to those groupings. ...

March 9, 2024

《架构整洁之道》:Chap6. FUNCTIONAL PROGRAMMING 函数式编程

In many ways, the concepts of functional programming predate programming itself. This paradigm is strongly based on the l-calculus invented by Alonzo Church in the 1930s. 函数式编程所依赖的原理,在很多方而其实是早于编程本身出现的。因为函数式编程这种范式强烈依赖于 Alonzo Church 在 20 世纪 30 年代发明的 λ 演算。 SQUARES OF INTEGERS 整数平方 To explain what functional programming is, it’s best to examine some examples. Let’s investigate a simple problem: printing the squares of the first 25 integers. 我们最好还是用一个例子来解释什么是函数式编程。请看下面的这个例子:这段代码想要输出前 25 个整数的平方值。 In a language like Java, we might write the following: ...

March 8, 2024

《架构整洁之道》:Chap5. OBJECT-ORIENTED PROGRAMMING 面向对象编程

As we will see, the basis of a good architecture is the understanding and application of the principles of object-oriented design (OO). But just what is OO? 稍后我们会讲到,设计一个优秀的软件架构要基于对面向对象设计(Object-Oriented Design)的深入理解及应用。但我们首先得弄明白一个问题:究竟什么是面向对象? One answer to this question is “The combination of data and function.” Although often cited, this is a very unsatisfying answer because it implies that o.f() is somehow different from f(o). This is absurd. Programmers were passing data structures into functions long before 1966, when Dahl and Nygaard moved the function call stack frame to the heap and invented OO. ...

March 7, 2024

《架构整洁之道》:Chap4. STRUCTURED PROGRAMMING 结构化编程

Edsger Wybe Dijkstra was born in Rotterdam in 1930. He survived the bombing of Rotterdam during World War II, along with the German occupation of the Netherlands, and in 1948 graduated from high school with the highest possible marks in math, physics, chemistry, and biology. In March 1952, at the age of 21 (and just 9 months before I was born), Dijkstra took a job with the Mathematical Center of Amsterdam as the Netherlands’ very first programmer. ...

March 6, 2024

《架构整洁之道》:Chap3. PARADIGM OVERVIEW 编程范式总览

The three paradigms included in this overview chapter are structured programming, object-orient programming, and functional programming. 本章将讲述三个编程范式,它们分别是结构化编程(structured programming)、 面向对象编程(object-oriented programming)以及函数式编程(functional programming)。 STRUCTURED PROGRAMMING 结构化编程 The first paradigm to be adopted (but not the first to be invented) was structured programming, which was discovered by Edsger Wybe Dijkstra in 1968. Dijkstra showed that the use of unrestrained jumps (goto statements) is harmful to program structure. As we’ll see in the chapters that follow, he replaced those jumps with the more familiar if/then/else and do/while/until constructs. ...

March 5, 2024