Design Principles, 设计原则之OCP

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:

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值