简介
开闭原则是编程过程中最基础、最重要的原则,前面讲到的几个原则,也都是为了实现开闭原则。
那么他是如何开放,又是如何关闭的呢?
- 一个软件实体,如类:模块和函数应该对扩展开放(提供方),对修改关闭(使用方)。
- 当软件需求变化时,尽量通过扩展软件实体来达到目的,而不是通过修改已有的代码来实现变化。
- 编程中遵循其他原则,以及使用设计模式的目的,就是遵循开闭原则。
实例展示
- 最初代码的逻辑:
- 代码如下:
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 shape){
if(shape.my_type==1){
drawRectangle();
}else if(shape.my_type==2){
drawCircle();
}else if(shape.my_type==3){ //新增的三角形
drawTriangle();
}
}
//绘制矩形
public void drawRectangle(){
System.out.println("绘制矩形");
}
//绘制圆形
public void drawCircle(){
System.out.println("绘制圆形");
}
//绘制三角形(这是新增的)
public void drawTriangle(){
System.out.println("绘制三角形");
}
}
//Shape类,基类
class Shape{
int my_type;
}
//矩形类,继承了Shape类
class Rectangle extends Shape{
Rectangle(){
super.my_type=1;
}
}
//圆形类,继承了Shape类
class Circle extends Shape{
Circle(){
super.my_type=2;
}
}
//新增图形(三角形),继承了Shape类
class Triangle extends Shape{
Triangle(){
super.my_type=3;
}
}
这一版的代码看起来比较容易理解,这是它的优点;但是违反了开闭原则,即对扩展开发,对修改关闭,在此代码中,我们给类增加新功能(增加三角形)的时候,修改了代码,破坏了开闭原则。
- 改进后的代码:
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{
//接收Shape对象,然后根据type,调用draw方法,来绘制不同的图形
public void drawShape(Shape shape){
shape.draw();
}
}
//Shape类,基类,这是一个抽象类
abstract class Shape{
int my_type;
//这是一个抽象方法
public abstract void draw();
}
//矩形类,继承了Shape类
class Rectangle extends Shape {
Rectangle(){
super.my_type=1;
}
//重写抽象类中的抽象方法
@Override
public void draw() {
System.out.println("绘制矩形");
}
}
//圆形类,继承了Shape类
class Circle extends Shape {
Circle(){
super.my_type=2;
}
@Override
public void draw() {
System.out.println("绘制圆形");
}
}
//新增图形(三角形),继承了Shape类
class Triangle extends Shape {
Triangle(){
super.my_type=3;
}
@Override
public void draw() {
System.out.println("绘制三角形");
}
}
//增加其他的图形
class OtherGraphic extends Shape{
OtherGraphic(){
super.my_type=4;
}
@Override
public void draw() {
System.out.println("绘制其他的图形");
}
}
- 改进后的方案:将基类定义为一个抽象类
Shape
,然后定义一个抽象方法draw()
,(让子类去实现)使各个图形类都继承这个抽象类Shape
并重写(实现)这个抽象方法draw()
,如果有新的需求,只需要再添加一个新的类,继承抽象类Shape
,并重写(实现)抽象方法draw()
即可,使用方的代码不用进行修改。
这么做,只是对功能进行了扩展,并没有对已有的代码进行修改,遵循了开闭原则。