当前位置:网站首页>软件设计七大原则之开闭原则(Open-Closed Principle, OCP)
软件设计七大原则之开闭原则(Open-Closed Principle, OCP)
2022-08-05 11:46:00 【夏至青柠】
活动地址:CSDN21天学习挑战赛
开闭原则
开闭原则(Open-Closed Principle, OCP)是指一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。所谓的开闭,也正是对扩展和修改两个行为的一个原则。强调的是用抽象构建框架,用实现扩展细节。可以提高软件系统的可复用性及可维护性。开闭原则,是面向对象设计中最基础的设计原则。它指导我们如何建立稳定灵活的系统。
例如:我们版本更新,我尽可能不修改源代码,但是可以增加新功能。
在现实生活中对于开闭原则也有体现。比如,很多互联网公司都实行弹性制作息时间,规定每天工作 8 小时。意思就是说,对于每天工作 8 小时这个规定是关闭的,但是你什么时候来,什么时候走是开放的。早来早走,晚来晚走。
实现开闭原则的核心思想就是面向抽象编程,接下来我们来看一段代码:以售卖课程为例,首先创建一个课程接口 ICourse
public interface ICourse {
Integer getId();
String getName();
Double getPrice();
}
整个课程生态有 Java 架构、大数据、人工智能、前端、软件测试等,我们来创建一个 Java 架构课程的类 JavaCourse:
public class JavaCourse implements ICourse{
private Integer id;
private String name;
private Double price;
public JavaCourse(Integer id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Integer getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Double getPrice() {
return this.price;
}
}
public class OpenCloseTest {
public static void main(String[] args) {
ICourse iCourse = new JavaCourse(1,"Java架构",11800D);
System.out.println("课程ID:" + iCourse .getId() +
"\n课程名称《" + iCourse .getName() + "》" +
"\n售价:" + iCourse .getPrice() );
}
}

现在我们要给 Java 架构课程做活动,价格优惠。如果修改 JavaCourse 中的 getPrice()方法,则会存在一定的风险,可能影响其他地方的调用结果。我们如何在不修改原有代码前提前下,实现价格优惠这个功能呢?现在,我们再写一个处理优惠逻辑的类,JavaDiscountCourse 类 ( 思 考 一 下 为 什 么 要 叫 JavaDiscountCourse , 而 不 叫DiscountCourse):
public class JavaDiscountCourse extends JavaCourse {
public JavaDiscountCourse(Integer id, String name, Double price) {
super(id, name, price);
}
public Double getDiscountPrice(){
return super.getPrice() * 0.6;
}
}
public class OpenCloseTest {
public static void main(String[] args) {
ICourse iCourse = new JavaDiscountCourse(1,"Java架构",11800D);
JavaDiscountCourse discountCourse = (JavaDiscountCourse)iCourse;
System.out.println("课程ID:" + discountCourse.getId() +
"\n课程名称《" + discountCourse.getName() + "》" +
"\n原价:" + discountCourse.getPrice() +
"\n售价:" + discountCourse.getDiscountPrice());
}
}

边栏推荐
- How about Ping An Mengwa Card Insurance?Let parents read a few ways to identify products
- 花的含义
- 常用的免费Api接口网址
- I'm going crazy.Again A few days can not be A problem
- Machine Learning - Logistic Regression
- Go 语言快速入门指南: 基本类型
- Mathcad 15.0软件安装包下载及安装教程
- 2022杭电多校联赛第六场 题解
- hdu4545 魔法串
- 365天挑战LeetCode1000题——Day 050 在二叉树中增加一行 二叉树
猜你喜欢

内存问题难定位,那是因为你没用ASAN

623. 在二叉树中增加一行 : 简单二叉树遍历运用题

2022年6月互联网医疗领域月度观察

5G NR system messages

Cesium.js 三维土壤地质剖面分割挖掘

D-Desthiobiotin-PEG4-Maleimide主要物理性质特点 !

Android 开发用 Kotlin 编程语言 二 条件控制

2022 CCF国际AIOps挑战赛决赛暨AIOps研讨会报名已开启

163_技巧_Power BI 一键批量建立自定义字段参数

Android development with Kotlin programming language II Conditional control
随机推荐
课表小程序使用攻略
Keras 分割网络自定义评估函数 - mean iou
365天挑战LeetCode1000题——Day 050 在二叉树中增加一行 二叉树
LeetCode brush questions (8)
Machine Learning - Logistic Regression
WingIDE 7.2.0 远程调试
What do T and Z in the time format 2020-01-13T16:00:00.000Z represent and how to deal with them
2022 CCF International AIOps Challenge Finals and AIOps Seminar Registration Open
STM32H743IIT6学习笔记02——USART
623. Add a row to a binary tree: Simple binary tree traversal problems
手把手教你定位线上MySQL慢查询问题,包教包会
消息中间件汇总
5G NR system messages
如何用Golang来手写一个Blog - Milu.blog 开发总结
Discover the joy of C language
I'm going crazy.Again A few days can not be A problem
Mysql8基础知识
Introduction to the Evolution of Data Governance System
冬日里,28℃的爱情
并非富人专属,一文让你对NFT改观