首页 > 其他分享 >类之OCP(Open Closed Principle):开闭原则

类之OCP(Open Closed Principle):开闭原则

时间:2023-06-09 17:04:06浏览次数:45  
标签:15 Server Principle Closed OCP 2012 class change PM


//z 2012-4-17 15:31:13 PM IS2120
1. OCP 是什么?
OCP : OPEN CLOSED PRINCIPLE
对扩展开放,对修改关闭
Software entities should be open for extension, but closed for modification
//z 2012-4-17 15:54:58 PM IS2120
1.1 扩展
通过继承、覆写、委托等实现新的功能

2. OCP的好处
通过扩展,满足新需求,使软件系统有灵活性
已有的软件模块,尤其是抽象层模块不再修改,使得软件具有稳定性和延续性
2.1 动机
架构稳定;又能满足变化中的需求
//z 2012-4-17 15:39:07 PM IS2120
2.2 为何需要OCP
重用(那些在变更中不变的部分)

//z 2012-4-17 15:34:37 PM IS2120
3. 如何实现?
抽象是解决问题的关键;抽象出不变的部分,与变化的部分相隔离。
//z 2012-4-17 15:53:08 PM IS2120
3.1 系统设计阶段
在系统设计阶段,对系统可能发生变化的部分进行评估和分类,每一个可变因素都单独进行封装。(理想情况;极端)

//z 2012-4-17 15:35:52 PM IS2120
4. 封装可变性
一种可变性应该存在于一个类中;而不是散落于代码各处
一种可变性不可与另一种可变性混合在一起;一般的继承层次不超过两层

//z 2012-4-17 15:37:49 PM IS2120
5. OCP
5.1 软件实体(类、模块等)应对扩展开放,而对修改闭合。
5.2 按语:对于已存的代码不需要进行变更;所有新的功能都能够通过增加新的派生类并覆写方法来实现
或是通过委托使用已存的代码来实现。
5.3 不向已存的代码引入新代码,就不会向其中引入新bug
5.4 使用抽象类,由类实现接口

//z 2012-4-17 15:46:12 PM IS2120
6. 关于OCP的对话
Shubho: Here goes the poster for the Open-Closed Principle:

Figure: Open Closed Principle poster

If I need to explain it in design oriented terms, it would be as follows:

"Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification."

扩展类的行为而不用修改类本身
At the most basic level, that means, you should be able to extend a class's behavior without modifying it. It's just like I should be able
to put on a dress without doing any change to my body, ha ha.

Farhana: Interesting. You can change your look by putting any dress you want, and you don't have to change your body for that. So you are
open for extension, right?

Shubho: Yes. In OOD, open for extensions means that the behavior of the module/class can be extended and we can make the module behave in
new and different ways as the requirements change, or to meet the needs of new applications.

Farhana: And your body is closed for modification. I like this example. So, the source code for a core class or module should not be
modified when it needs an extension. Can you explain with some examples?

Shubho: Sure, take a look at the following example. This doesn't support the "Open-Closed" principle:

Class hierarchy showing violation of Open Closed principle

client->server

You see, the Client and Server classes are both concrete. So, if for any reason, the server implementation is changed, the client also
needs a change.

Farhana: Makes sense. If a browser is implemented as tightly coupled to a specific server (such as IIS), then if the server is replaced
for some reason with another one (say, Apache) the browser also needs to be changed or replaced. That would be really horrible!

Shubho: Correct. So following would be the correct design:

Class hierarchy showing Open Closed Principle

client -> abstract server -- server

In this example, there is an Abstract Server class added, and the client holds a reference to the abstract class, and the Concrete Server
class implements the Abstract Server class. So, if for any reason the Server implementation is changed, the Client is not likely to require
 any change.

The Abstract Server class here is closed for modification, and the concrete class implementations here are open for extension.

抽象是关键;增加一个中间层
Farhana: As I understand, abstraction is the key, right?

抽象系统的核心概念,并作为双方交互的一个接口(类似TCP的各层设计)
Shubho: Yes, basically, you abstract the things that are the core concepts of your system, and if you do the abstraction well, most likely
, it would require no change when the functionality is to be extended (such as the server is an abstract concept). And, you define the
abstract things in the implementations (say, IISServer implements the Server) and code against abstractions (Server) as much as possible.
This would allow you to extend the abstract thing and define a new implementation (say, ApacheServer) without doing any change in the
client code.

标签:15,Server,Principle,Closed,OCP,2012,class,change,PM
From: https://blog.51cto.com/u_16156420/6449432

相关文章

  • github报错“ssh_exchange_identification: Connection closed by remote host fatal:
    解决方式:不确定是否为密钥过期还是C:\Users\John\.ssh文件夹下的config文件中没有添加github的host。总之,所有的尝试如下:(1)重新生成公钥和私钥。打开gitbash,输入以下命令获取自己github的email。gitconfiguser.email 生成ssh私钥和公钥ssh-keygen-trsa-......
  • MATLAB代码:配网节点电价 DLMP 关键词:DLMP SOCP lindistflow
    MATLAB代码:配网节点电价DLMP关键词:DLMPSOCPlindistflow参考文档:《DistributionLocationalMarginalPricing(DLMP)forCongestionManagementandVoltageSupport》2018SCI一区IEEETransactionsonPowerSystem非完美复现仿真平台:MATLAB+Gurobi主要内容:1.考虑网......
  • MATLAB代码:Cplex二阶锥规划最优潮流入门 关键词:二阶锥 最优潮流 SOCP
    MATLAB代码:Cplex二阶锥规划最优潮流入门关键词:二阶锥最优潮流SOCP参考文档:《主动配电网最优潮流研究及其应用实例》仿真平台:MATLAB+Cplex主要内容:最优潮流研究在配电网规划运行中不可或缺,且在大量分布式能源接入的主动配电网环境下尤为重要。传统的启发式算法在全局最优解和......
  • Principle:专为Mac设计的UI原型设计软件
    PrincipleMac是专为Mac设计UI原型设计软件。在使用PrincipleMac时,您可以快速创建各种主题,让他们与电脑相连接。在编辑界面上,您可以使用Photoshop和其他绘图工具编辑应用,包括图片样式和文本样式。如果你的工作是在计算机上创建原型设计的话,那么它能为您提供令人难以置信的方便......
  • ssh远程连接报错ssh_exchange_identification: Connection closed by remote host
    被远程主机拒绝此类报错为原因1:ssh连接数量过多导致如果问题是偶尔能登录一次,大多不能登录,建议往第一点方向排查[root@localhost~]#cat/etc/ssh/sshd_config|grepMaxSessions#MaxSessions10[root@localhost~]#cat/etc/ssh/sshd_config|grepMaxStart#MaxStartups10......
  • IOCP
    本文主要探讨一下windows平台上的完成端口开发及其与之相关的几个重要的技术概念,这些概念都是与基于IOCP的开发密切相关的,对开发人员来讲,又不得不给予足够重视的几个概念:1)基于IOCP实现的服务吞吐量2)IOCP模式下的线程切换3)基于IOCP实现的消息的乱序问题。 一、IOCP简介 ......
  • The principle of uploading files with command line tools All In One
    TheprincipleofuploadingfileswithcommandlinetoolsAllInOne命令行工具文件上传的原理/TheprincipleofcommandlinetoolfileuploaddemospipgitCDNOSS{"name":"xui","version":"1.0.0","main&q......
  • 目录操作函数(mkdir rmdir rename chdir getcwd opendir closedir readdir dup dup2)
      /*创建文件#include<sys/stat.h>#include<sys/types.h>intmkdir(constchar*pathname,mode_tmode);参数:mode:权限移除文件#include<unistd.h>intrmdir(constc......
  • ChatGPT人工智能热潮之下,NCSI功能OCP网卡助力数据中心发展
    继美国OpenAI发布ChatGPT后,百度召开“文心一言”发布会,引发了新一轮人工智能热潮。此次狂潮的兴起,进一步拉动算力需求的高增长,推动数据中心朝着高技术、高算力、高能效、高安全发展。服务器作为数据中心的基础硬件设施之一,高扩展性和易管理性能够加速推进数据中心的建设和发展。......
  • 基于时间和空间的大规模电动汽车入网网损调度 建立MISOCP模型,分时段优化,并行计算(实时
    基于时间和空间的大规模电动汽车入网网损调度建立MISOCP模型,分时段优化,并行计算(实时优化)。并对比了优化和未优化结果,验证了调度的有效性。考虑到电动汽车的机动性,市区可分为三类功能区:住宅区,商业区和办公区。白天,大多数电动汽车都停在办公室区域。到了晚上,几乎所有的电动汽车......