基本介绍
- 开闭原则是编程中最基础,最重要的设计原则。
- 一个软件实体如类,模块和函数应该对扩展开放(对提供方),对修改关闭(对使用方)。用抽象构建框架,用实现扩展细节。
- 当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。
- 编程中遵循其他原则,以及使用设计模式的目的就是遵循开闭原则。
实例分析
模拟简单画图功能:
public class Shape {
int type;
}
public class Rectangle extends Shape{
Rectangle() {
super.type = 1;
}
}
public class Circle extends Shape{
Circle() {
super.type = 2;
}
}
class GraphicEditor {
public void drawShape(Shape s) {
if (s.type == 1) {
drawRectangle(s);
}
else if (s.type == 2) {
drawCircle(s);
}
}
public void drawRectangle(Shape s) {
System.out.println("矩形");
}
public void drawCircle(Shape s) {
System.out.println("圆形");
}
}
public static void main(String[] args) {
GraphicEditor graphicEditor = new GraphicEditor();
graphicEditor.drawShape(new Circle());
graphicEditor.drawShape(new Rectangle());
}
假如现在需要增加一个绘制三角形的功能,则需要进行如下更改:
public class Shape {
int type;
}
public class Rectangle extends Shape{
Rectangle() {
super.type = 1;
}
}
public class Circle extends Shape{
Circle() {
super.type = 2;
}
}
public class Triangle extends Shape{
Triangle() {
super.type = 3;
}
}
class GraphicEditor {
public void drawShape(Shape s) {
if (s.type == 1) {
drawRectangle(s);
}
else if (s.type == 2) {
drawCircle(s);
} else if (s.type == 3) {
drawTriangle(s);
}
}
public void drawRectangle(Shape s) {
System.out.println("矩形");
}
public void drawCircle(Shape s) {
System.out.println("圆形");
}
public void drawTriangle(Shape s) {
System.out.println("三角形");
}
}
public static void main(String[] args) {
GraphicEditor graphicEditor = new GraphicEditor();
graphicEditor.drawShape(new Circle());
graphicEditor.drawShape(new Rectangle());
graphicEditor.drawShape(new Triangle());
}
问题分析:
可以看到当增加新功能的时候需要修改的地方很多,不仅服务的提供方需要更改(增加Triangle类),而且服务的使用方也需要更改(需要增加if-else判断,主函数中也需要添加代码)。虽然这种方式容易理解,容易操作,但是缺点也很明显,违背了开闭原则。
改进思路:
把创建的Shape类做成抽象类,并且提供一个抽象的draw方法,让子类去实现即可,这样我们有新的图形种类的时候,只需要让新的图形继承Shape即可,使用方的代码不需要修改,满足了开闭原则。
改进实现如下:
public abstract class Shape {
int type;
public abstract void draw();
}
public class Rectangle extends Shape {
@Override
public void draw() {
System.out.println("矩形");
}
}
public class Circle extends Shape {
@Override
public void draw() {
System.out.println("圆形");
}
}
public class Triangle extends Shape {
@Override
public void draw() {
System.out.println("三角形");
}
}
class GraphicEditor {
public void drawShape(Shape s) {
s.draw();
}
}
public static void main(String[] args) {
GraphicEditor graphicEditor = new GraphicEditor();
graphicEditor.drawShape(new Rectangle());
graphicEditor.drawShape(new Triangle());
graphicEditor.drawShape(new Circle());
}
在这种方式下,如果需要添加新的功能,只需要添加一个新的类去继承Shape类,重写里面的draw方法即可,不需要进行过多的修改。即满足对扩展开放(对提供方),对修改关闭(对使用方)。