In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章将为大家详细讲解有关Java中什么是开闭原则,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
1.什么是开闭原则?
开闭原则(Open Closed Principle)是编程中最基础、最重要的设计原则。
一个软件实体如类,模块和函数应该对扩展开放(对提供方),对修改关闭(对使用方)。用抽象构建框架,用实现扩展细节。
当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。
编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则。
2.违反Ocp代码案例
package com.szh.principle.ocp; /** * *///Shape类,基类class Shape { int m_type;} class Rectangle extends Shape { Rectangle() { super.m_type = 1; }} class Circle extends Shape { Circle() { super.m_type = 2; }} //这是一个用于绘图的类 [使用方]class GraphicEditor { //接收Shape对象,然后根据type,来绘制不同的图形 public void drawShape(Shape s) { if (s.m_type == 1) drawRectangle(s); else if (s.m_type == 2) drawCircle(s); } //绘制矩形 public void drawRectangle(Shape r) { System.out.println(" 绘制矩形 "); } //绘制圆形 public void drawCircle(Shape r) { System.out.println(" 绘制圆形 "); }} public class Ocp { public static void main(String[] args) { //使用看看存在的问题 GraphicEditor graphicEditor = new GraphicEditor(); graphicEditor.drawShape(new Rectangle()); graphicEditor.drawShape(new Circle()); }}
根据上面的代码及运行结果来看,没一点问题,我们如愿的画出了矩形、圆形。但是现在有了一个新的需求,说 要增添一个图形(三角形),使代码完成对三角形的绘制,那么我们就需要对上面的代码进行修改。
package com.szh.principle.ocp; /** * *///Shape类,基类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; }} //这是一个用于绘图的类 [使用方]class GraphicEditor { //接收Shape对象,然后根据type,来绘制不同的图形 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(" 绘制三角形 "); }} 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 类中也做了修改。 这就明显违反了开闭原则中的 对修改关闭 这个规则。
我们需要的是 对扩展开放,对修改关闭 的规则,也就是说增添一个三角形的时候,我们只需要在提供方做修改,在使用方是无需修改的。
也就是说,当我们给一个类增添新的功能时,尽量不修改代码,或者是尽量少的修改代码。
3.遵守Ocp代码案例
思路: 把创建Shape类做成抽象类,并提供一个抽象的draw方法,让子类去实现即可,这样我们有新的图形种类时,只需要让新的图形类继承Shape,并实现 draw方法即可,使用方的代码就不需要修 → 满足了开闭原则。
package com.szh.principle.ocp.improve; /** * *///Shape类,基类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 GraphicEditor { //接收Shape对象,调用draw方法 public void drawShape(Shape s) { s.draw(); }} public class Ocp { public static void main(String[] args) { GraphicEditor graphicEditor = new GraphicEditor(); graphicEditor.drawShape(new Rectangle()); graphicEditor.drawShape(new Circle()); }}
上面是改进之后的代码,此时我们还像之前的案例一样,增添一个新的图形(三角形),并完成对三角形的绘制,那么对上面代码的修改就少之又少了。
修改代码如下:????????????
package com.szh.principle.ocp.improve; /** * *///Shape类,基类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 GraphicEditor { //接收Shape对象,调用draw方法 public void drawShape(Shape s) { s.draw(); }} 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()); }}
可以看到,我们对提供方代码中新增了一个 Triangle 类,它来完成对三角形的绘制。而自始至终我们的使用方 GraphicEditor 类都没有做任何的修改。
这就自然而然的满足了开闭原则中的 对扩展开发、对修改关闭 了。
关于"Java中什么是开闭原则"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.