1. The Open Closed Principle (OCP)
A module should be open for extension but closed for modification.
Of all the principles of object oriented design, this is the most important. It originated
from the work of Bertrand Meyer. It means simply this: We should write our modules
so that they can be extended, without requiring them to be modified. In other
words, we want to be able to change what the modules do, without changing the
source code of the modules.
open/close 原则
一个模块应该允许可扩展,而不允许修改。在所有面向对象设计的原则中,这是最重要的。它起源于Bertrand Meyer的工作中。它仅仅意味着:我们应该设计我们的模块,允许它们被扩展,而不允许他们被修改。换句话说,我们需要在不改变模块源代码的基础上,改变模块作的模块作的内容。
This may sound contradictory, but there are several techniques for achieving the OCP
on a large scale. All of these techniques are based upon abstraction. Indeed, abstraction
is the key to the OCP.
这可能听起来比较矛盾,但是有几种技巧可以实现OCP,所有的这些技巧来源于抽象,事实上,抽象是OCP的关键。
As an example of the OCP, here the LogOn function depends
only upon the Modem interface. Additional modems will not cause the LogOn function
to change. Thus, we have created a module that can be extended, with new
modems, without requiring modification.
作为一个OCP的例子。这儿LogOn功能仅仅依赖于Modem接口。增加的modems不会影响到LogOn功能改变。这样,我们就创建了一个允许扩展的模块,不需要修改就可以应用新的modem。
See below: