| 1) 开闭原则(Open Closed Principle)是编程中最基础、最重要的设计原则 |
| 2) 一个软件实体如类,模块和函数应该对扩展开放(对提供方),对修改关闭(对使用方)。用抽象构建框架,用实现扩展细节。 |
| 3) 当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。 |
| 4) 编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则。 |
| # 未使用开闭原则 |
| package com.atguigu.principle.ocp; |
| |
| public class Ocp { |
| |
| public static void main(String[] args) { |
| |
| GraphicEditor graphicEditor = new GraphicEditor(); |
| graphicEditor.drawShape(new Rectangle()); |
| graphicEditor.drawShape(new Circle()); |
| graphicEditor.drawShape(new Triangle()); |
| } |
| |
| } |
| |
| |
| class GraphicEditor { |
| |
| public void drawShape(Shape s) { |
| if (s.m_type == 1) |
| drawRectangle(s); |
| else if (s.m_type == 2) |
| drawCircle(s); |
| else if (s.m_type == 3) |
| drawTriangle(s); |
| } |
| |
| |
| public void drawRectangle(Shape r) { |
| System.out.println(" 绘制矩形 "); |
| } |
| |
| |
| public void drawCircle(Shape r) { |
| System.out.println(" 绘制圆形 "); |
| } |
| |
| |
| public void drawTriangle(Shape r) { |
| System.out.println(" 绘制三角形 "); |
| } |
| } |
| |
| |
| class Shape { |
| int m_type; |
| } |
| |
| class Rectangle extends Shape { |
| Rectangle() { |
| super.m_type = 1; |
| } |
| } |
| |
| class Circle extends Shape { |
| Circle() { |
| super.m_type = 2; |
| } |
| } |
| |
| |
| class Triangle extends Shape { |
| Triangle() { |
| super.m_type = 3; |
| } |
| } |
| 1) 优点是比较好理解,简单易操作。 |
| 2) 缺点是违反了设计模式的ocp原则,即对扩展开放(提供方),对修改关闭(使用方)。 |
| 即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码. |
| 3) 比如我们这时要新增加一个图形种类 三角形,我们需要做如下修改,修改的地方较多 |
| 把创建Shape类做成抽象类,并提供一个抽象的draw方法,让子类去实现即可,这样我们有新的图形种类时,只需要让新的图形类继承Shape,并实现draw方法即可,使用方的代码就不需要修 -> 满足了开闭原则 |
| package com.atguigu.principle.ocp.improve; |
| |
| public class Ocp { |
| |
| public static void main(String[] args) { |
| |
| GraphicEditor graphicEditor = new GraphicEditor(); |
| graphicEditor.drawShape(new Rectangle()); |
| graphicEditor.drawShape(new Circle()); |
| graphicEditor.drawShape(new Triangle()); |
| graphicEditor.drawShape(new OtherGraphic()); |
| } |
| |
| } |
| |
| |
| class GraphicEditor { |
| |
| public void drawShape(Shape s) { |
| s.draw(); |
| } |
| } |
| |
| |
| abstract class Shape { |
| int m_type; |
| |
| public abstract void draw(); |
| } |
| |
| class Rectangle extends Shape { |
| Rectangle() { |
| super.m_type = 1; |
| } |
| |
| @Override |
| public void draw() { |
| |
| System.out.println(" 绘制矩形 "); |
| } |
| } |
| |
| class Circle extends Shape { |
| Circle() { |
| super.m_type = 2; |
| } |
| @Override |
| public void draw() { |
| |
| System.out.println(" 绘制圆形 "); |
| } |
| } |
| |
| |
| class Triangle extends Shape { |
| Triangle() { |
| super.m_type = 3; |
| } |
| @Override |
| public void draw() { |
| |
| System.out.println(" 绘制三角形 "); |
| } |
| } |
| |
| |
| class OtherGraphic extends Shape { |
| OtherGraphic() { |
| super.m_type = 4; |
| } |
| |
| @Override |
| public void draw() { |
| |
| System.out.println(" 绘制其它图形 "); |
| } |
| } |
- 参考

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构