基本介绍
- 开闭原则(Open Closed Principle)是编程中最基础、最重要的设计原则
- 一个软件实体,如类,模块和函数应该对扩展开放(提供方),对修改关闭(使用方)。用抽象构建框架,用实现扩展细节
- 当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化
- 编程中遵循其他原则,以及使用设计模式的目的就是遵循开闭原则
应用实例
方式1
/**
* Created with IntelliJ IDEA.
* User: Mingda
* Time: 2024/2/27 10:45
* File: Ocp
* Description: 开闭原则
*/
public class Ocp {
public static void main(String[] args) {
GraphicEditor graphicEditor = new GraphicEditor();
graphicEditor.drawShape(new Rectangle());
graphicEditor.drawShape(new Circle());
}
}
// 绘图类
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("draw rectangle");
}
public void drawCircle(Shape c) {
System.out.println("draw circle");
}
}
// Shape基类
class Shape {
int m_type;
}
class Rectangle extends Shape {
Rectangle() {
super.m_type = 1;
}
}
class Circle extends Shape {
Circle() {
super.m_type = 2;
}
}
优缺点:
- 通俗易懂,好上手
- 违反了设计模式的开闭原则(ocp),即对扩展开放(提供方),对修改关闭(使用方)。就是当我们给类增加新功能时,尽量不修改代码,或者尽可能少修改代码
- 比如此时需要增加一个新图形种类,修改的地方较多,如下:
/**
* Created with IntelliJ IDEA.
* User: Mingda
* Time: 2024/2/27 10:45
* File: Ocp
* Description: 开闭原则
*/
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 {
// 接收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("draw rectangle");
}
// 绘制圆形
public void drawCircle(Shape c) {
System.out.println("draw circle");
}
// 绘制三角形
public void drawTriangle(Shape t) {
System.out.println("draw triangle");
}
}
// 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;
}
}
改进思路
把创建Shape类做成抽象类,并提供一个抽象的draw方法,让子类去实现即可。这样子当有新的图形种类时,只需要让新的图形类继承Shape,并实现draw方法即可。 ->使用方的代码不改动,满足了开闭原则
改进代码
/**
* Created with IntelliJ IDEA.
* User: Mingda
* Time: 2024/2/27 10:45
* File: Ocp
* Description: 开闭原则
*/
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 {
// 接收Shape对象,调用draw()方法
public void drawShape(Shape shape) {
shape.draw();
}
}
// 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("draw rectangle");
}
}
class Circle extends Shape {
Circle() {
super.m_type = 2;
}
@Override
public void draw() {
System.out.println("draw circle");
}
}
// 新增三角形
class Triangle extends Shape {
Triangle() {
super.m_type = 3;
}
@Override
public void draw() {
System.out.println("draw triangle");
}
}
再新增一个其他图形进行测试:
/**
* Created with IntelliJ IDEA.
* User: Mingda
* Time: 2024/2/27 10:45
* File: Ocp
* Description: 开闭原则
*/
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 Line());
}
}
// 绘图类(使用方)
class GraphicEditor {
// 接收Shape对象,调用draw()方法
public void drawShape(Shape shape) {
shape.draw();
}
}
// 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("draw rectangle");
}
}
class Circle extends Shape {
Circle() {
super.m_type = 2;
}
@Override
public void draw() {
System.out.println("draw circle");
}
}
// 新增三角形
class Triangle extends Shape {
Triangle() {
super.m_type = 3;
}
@Override
public void draw() {
System.out.println("draw triangle");
}
}
// 新增一个其他的图形
class Line extends Shape {
Line() {
super.m_type = 4;
}
@Override
public void draw() {
System.out.println("draw line");
}
}
满足ocp