我的问题:我们做某个东西,确实可以划分为俩个部分,这个东西有什么用,这个东西是怎么做的,该怎么用。
提供什么能力"(机制) 和 "如何使用这些能力"(策略)
在我意识里 机制跟策略是近义词,这样形容,我就不理解了。
机制和策略的区分是其中一个在 Unix 设计背后的最好观念. 大部分的编程问题其实可以
划分为 2 部分:" 提供什么能力"(机制) 和 "如何使用这些能力"(策略). 如果这两方面
由程序的不同部分来表达, 或者甚至由不同的程序共同表达, 软件包是非常容易开发和适
应特殊的需求.
例如, 图形显示的 Unix 管理划分为 X 服务器, 它理解硬件以及提供了统一的接口给用
户程序, 还有窗口和会话管理器, 它实现了一个特别的策略, 而对硬件一无所知. 人们可
以在不同的硬件上使用相同的窗口管理器, 而且不同的用户可以在同一台工作站上运行不
同的配置. 甚至完全不同的桌面环境, 例如 KDE 和 GNOME, 可以在同一系统中共存. 另
一个例子是 TCP/IP 网络的分层结构: 操作系统提供 socket 抽象层, 它对要传送的数据
The distinction between mechanism and policy is one of the best ideas behind the
Unix design. Most programming problems can indeed be split into two parts: “what
capabilities are to be provided” (the mechanism) and “how those capabilities can be
used” (the policy). If the two issues are addressed by different parts of the program,
or even by different programs altogether, the software package is much easier to
develop and to adapt to particular needs.
For example, Unix management of the graphic display is split between the X server,
which knows the hardware and offers a unified interface to user programs, and the
window and session managers, which implement a particular policy without know-
ing anything about the hardware. People can use the same window manager on dif-
ferent hardware, and different users can run different configurations on the same
workstation. Even completely different desktop environments, such as KDE and
GNOME, can coexist on the same system. Another example is the layered structure
of TCP/IP networking: the operating system offers the socket abstraction, which
implements no policy regarding the data to be transferred, while different servers are
in charge of the services (and their associated policies). Moreover, a server like ftpd
provides the file transfer mechanism, while users can use whatever client they prefer;
both command-line and graphic clients exist, and anyone can write a new user inter-
face to transfer files.