开闭原则(open-closed principle)

开闭原则是由Bertrand Meyer于1988年提出的,当时的描述如下:
SOFTWARE ENTITIES(CLASSES,MODULES,FUNCTIONS, ETC.)SHOULD BE OPEN FOR EXTENSION , BUT CLOSED FOR MODIFICATION.
Modules that conform to the open-closed principle have two primary attributes.
1. They are “Open For Extension”.
   This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.
2. They are “Closed for Modification”.
    The source code of such a module is inviolate. No one is allowed to make source code changes to it.
开闭原则要求在系统的逻辑要求发生变化时,可以增加新的entity(class、modules或者function等)来实现新的部分逻辑,但是不能修改已经存在的entity。
    programs that conform to the open-closed principle are changed by adding new code, rather than by changing existing code, they do not experience the cascade of changes exhibited by non-conforming programs.
没有一个程序能够绝对的满足开闭原则,只能做到对某个特性是满足这个原则的。在考虑开闭原则时不是去考虑什么会导致设计改变,而是考虑你允许什么(哪个特性)发生变化而不让这一变化导致重新设计。总结而言就是:找到一个系统的可变因素,将它封装起来,并命名为”对可变性的封装原则“。
开闭原则不允许更改系统的抽象层,而允许扩展系统从实现层,所以,实现开闭原则的关键在于抽象。

参考:
《Java与模式》,阎宏编著
《The Open-Closed Principle》,by Robert C. Martin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值