设计模式——开闭原则

本文通过一个简单的画图功能模拟案例,展示了如何违反开闭原则,导致代码修改困难。然后,通过引入抽象类和接口,实现了对扩展开放、对修改关闭的设计,遵循了开闭原则。改进后的代码在添加新图形类型时无需修改原有代码,提高了代码的可维护性和扩展性。
摘要由CSDN通过智能技术生成

基本介绍

  1. 开闭原则是编程中最基础,最重要的设计原则。
  2. 一个软件实体如类,模块和函数应该对扩展开放(对提供方),对修改关闭(对使用方)。用抽象构建框架,用实现扩展细节。
  3. 当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。
  4. 编程中遵循其他原则,以及使用设计模式的目的就是遵循开闭原则。

实例分析

模拟简单画图功能:

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方法即可,不需要进行过多的修改。即满足对扩展开放(对提供方),对修改关闭(对使用方)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值