《架构整洁之道》: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

《架构整洁之道》:Part2. STARTING WITH THE BRICKS: PROGRAMMING PARADIGMS 从基础构件开始:编程范式

Software architecture begins with the code—and so we will begin our discussion of architecture by looking at what we’ve learned about code since code was first written. 任何软件架构的实现都离不开具体的代码,所以我们对软件架构的讨论应该从第一行被写下的代码开始。 In 1938, Alan Turing laid the foundations of what was to become computer programming. He was not the first to conceive of a programmable machine, but he was the first to understand that programs were simply data. By 1945, Turing was writing real programs on real computers in code that we would recognize (if we squinted enough). Those programs used loops, branches, assignment, subroutines, stacks, and other familiar structures. Turing’s language was binary. ...

March 4, 2024

《架构整洁之道》:Chap2. A TALE OF TWO VALUES 两个价值维度

Every software system provides two different values to the stakeholders: behavior and structure. Software developers are responsible for ensuring that both those values remain high. Unfortunately, they often focus on one to the exclusion of the other. Even more unfortunately, they often focus on the lesser of the two values, leaving the software system eventually valueless. 对于每个软件系统,我们都对以通过行为和架构两个维度来休现它的实际价值。软件研发人员应该确保自己的系统在这两个维度上的实际价值都能长时间维持在很高的状态。不幸的是,他们往往只关注一个维度,而忽视了另外一个维度。更不幸的是,他们常常关注的还是错误的维度,这导致了系统的价值最终趋降为零。 BEHAVIOR 行为价值 The first value of software is its behavior. Programmers are hired to make machines behave in a way that makes or saves money for the stakeholders. We do this by helping the stakeholders develop a functional specification, or requirements document. Then we write the code that causes the stakeholder’s machines to satisfy those requirements. ...

March 3, 2024

《架构整洁之道》:Chap1. WHAT IS DESIGN AND ARCHITECTURE? 设计与架构到底是什么

There has been a lot of confusion about design and architecture over the years. What is design? What is architecture? What are the differences between the two? 一直以来,设计(Design)与架构(Architecture)这两个概念让大多数人十分迷惑——什么是设计?什么是架构?二者究竟有什么区别? One of the goals of this book is to cut through all that confusion and to define, once and for all, what design and architecture are. For starters, I’ll assert that there is no difference between them. None at all. 本书的一个重要的目标就是要清晰、明确地对二者进行定义。首先我要明确地说,二者没有任何区别。一丁点区别都没有! The word “architecture” is often used in the context of something at a high level that is divorced from the lower-level details, whereas “design” more often seems to imply structures and decisions at a lower level. But this usage is nonsensical when you look at what a real architect does. ...

March 2, 2024

《架构整洁之道》:AFTERWORD 后序

My professional career as a software developer began in the 1990s, at a time when the dinosaurs of Big Architecture ruled the world. To get ahead, you had to learn about objects and components, about design patterns, and about the Unified Modeling Language (and its precursors). 我的软件工程师生涯开始于 20 世纪 90 年代,那是一个恐龙级大型架构统治世界的时代。要想在那样的时代获得一席之地,我们必须学会对象及其组件、设计模式、统一建模语言(包括其前身)的相关知识。 Projects—and boy, should we rue the day when we decided to call them that?—started with long design phases, where detailed blueprints for our systems were laid out by “senior” programmers for more “junior” programmers to follow. Which, of course, they didn’t. Ever. ...

March 1, 2024