zoukankan      html  css  js  c++  java
  • 设计模式系列(五)七大设计原则-----开闭原则

    开闭原则

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

    案例

    1. 如下 uml 类图
      在这里插入图片描述
    2. 代码实现
    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(" 绘制矩形");
    	}
    	//绘制圆形
    	public void drawCircle(Shape r) {
    		System.out.println(" 绘制圆形");
    	}
    	
    	//绘制三角形
    	public void drawTriangle(Shape r) {
    		System.out.println(" 绘制三角形");
    	}
    }
    
    //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;
    	}
    }
    
    1. 说明
    • 优点就是比较好理解,简单易操作
    • 缺点就是违反了设计模式的 ocp 原则,即对扩展开放(提供方),对修改关闭(使用方)。即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码
    • 比如我们这时要新增加一个图形种类 三角形。修改的地方比较多

    改进

    思路:把创建 Shape类改为抽象类,并提供一个 抽象的draw方法,让子类去实现即可 ,这样我们有新的图形种类的时候,只需要让新的图形类继承 Shape 类,并实现 draw 方法即可,适用方的代码就不需要修改,满足了开闭原则

    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 对象,调用draw 方法
    	public void drawShape(Shape s) {
    		s.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(" 绘制矩形");
    	}
    }
    
    class Circle extends Shape {
    	Circle() {
    		super.m_type = 2;
    	}
    	@Override
    	public void draw() {
    		System.out.println(" 绘制圆形");
    	}
    }
    
    //新增画三角形
    class Triangle extends Shape {
    	Triangle() {
    		super.m_type = 3;
    	}
    	@Override
    	public void draw() {
    		System.out.println(" 绘制三角形");
    	}
    }
    
    //新增一个图形
    class OtherGraphic extends Shape {
    	OtherGraphic() {
    		super.m_type = 4;
    	}
    	@Override
    	public void draw() {
    		System.out.println(" 绘制其它图形");
    	}
    }
    
  • 相关阅读:
    ASP.NET DropDownList 控件绑定数据
    Linux图形界面与命令行界面切换
    Linux性能分析
    UTF虚拟对象
    UFT场景恢复
    UFT参数化
    UFT检查点
    UFT三种录制方式
    UFT基本操作
    UFT安装目录简单介绍
  • 原文地址:https://www.cnblogs.com/shanggushenlong/p/13801320.html
Copyright © 2011-2022 走看看