设计模式-开闭原则介绍

开闭原则介绍

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

方案一

public class ocp1 {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        //绘制矩形
        graphicEditor.drawShape(new Rectangle());
        //绘制圆形
        graphicEditor.drawShape(new Circle());
    }
}

/**
 * 绘制一个图形
 */
class GraphicEditor {
    //接受Shap,根据type绘制不同图形
    public void drawShape(Shap shap) {
        if (shap.m_type == 1) {
            drawRectangle();
        }

        if (shap.m_type == 2) {
            drawCircle();
        }
    }

    public void drawRectangle() {
        System.out.println("矩形");
    }

    public void drawCircle() {
        System.out.println("圆形");
    }
}

/**
 * 基类Shap
 */
class Shap {
    int m_type;
}

/**
 * 继承Shap,使用type = 1
 */
class Rectangle extends Shap {
    Rectangle() {
        super.m_type = 1;
    }
}

/**
 * 继承Shap,使用不同的type = 2
 */
class Circle extends Shap {
    Circle() {
        super.m_type = 2;
    }
}
1)简单易操,理解简单
2)违反了开闭原则,即对扩展开放(对程序员方),对修改关闭(对使用方)
3)我们需要增加一个会组织三角形时,需要修改很多地方 => 方案二

方案二

public class ocp1 {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        //绘制矩形
        graphicEditor.drawShape(new Rectangle());
        //绘制圆形
        graphicEditor.drawShape(new Circle());
        //绘制三角形
        graphicEditor.drawShape(new Triangle());
    }
}

/**
 * 绘制一个图形【使用方新增方法,和进行了修改】
 * 违背了开闭原则
 * 改进 : 把基类Shap做成抽象类,提供一个draw方法,让子类去实现即可,使用方的代码不需要修改,满足开闭原则
 * 解决 : 方案三
 */
class GraphicEditor {
    //接受Shap,根据type绘制不同图形
    public void drawShape(Shap shap) {
        if (shap.m_type == 1) {
            drawRectangle();
        }

        if (shap.m_type == 2) {
            drawCircle();
        }

        // type = 3绘制三角形
        if (shap.m_type == 3) {
            drawTriangle();
        }
    }

    public void drawRectangle() {
        System.out.println("矩形");
    }

    public void drawCircle() {
        System.out.println("圆形");
    }
    
    //绘制三角形
    public void drawTriangle() {
        System.out.println("三角形");
    }
}

/**
 * 基类Shap
 */
class Shap {
    int m_type;
}

/**
 * 继承Shap,使用type = 1
 */
class Rectangle extends Shap {
    Rectangle() {
        super.m_type = 1;
    }
}

/**
 * 继承Shap,使用不同的type = 2
 */
class Circle extends Shap {
    Circle() {
        super.m_type = 2;
    }
}

/**
 * 新增三角形,继承Shap,使用不同的type = 3
 */
class Triangle extends Shap {
    Triangle() {
        super.m_type = 3;
    }
}

方案三

public class ocp2 {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        //绘制矩形
        graphicEditor.drawShape(new Rectangle());
        //绘制圆形
        graphicEditor.drawShape(new Circle());
        //绘制三角形,只需扩展一个类
        graphicEditor.drawShape(new Triangle());
    }
}

/**
 * 新增图形不需修改使用方
 */
class GraphicEditor {
    //接受Shap,根据type绘制不同图形
    public void drawShape(Shap shap) {
		//只需调用一个方法
        shap.draw();
    }
}

/**
 * 基类Shap
 */
abstract class Shap {

    //抽象方法
    public abstract void draw();
}

/**
 * 继承Shap,实现抽象方法
 */
class Rectangle extends Shap {

    @Override
    public void draw() {
        System.out.println("矩形");
    }
}

/**
 * 继承Shap,实现抽象方法
 */
class Circle extends Shap {

    @Override
    public void draw() {
        System.out.println("圆形");
    }
}

/**
 * 新增三角形,继承Shap,实现抽象方法
 * 扩展开放
 */
class Triangle extends Shap {

    @Override
    public void draw() {
        System.out.println("三角形");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值