Java设计模式七大原则-开闭原则
开闭原则
- 开闭原则(Open Closed Principle)是编程中最基础、最重要的设计原则
- 一个软件实体如类,模块和函数应该对扩展开放(对提供方),对修改关闭(对使用方)。用抽象构建框架,用实现扩展细节。
- 当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。
- 编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则。
例子
public class Ocp {public static void main(String[] args) {GraphicEditor graphicEditor = new GraphicEditor();graphicEditor.drawCircle(new Circle());graphicEditor.drawRectangle(new Rectangle());}
}
//用于绘图的类
class GraphicEditor {//接收不同的对象并调用相应的方法public void drawShape(Shape s) {if (s.m_type == 1)drawRectangle(s);else if (s.m_type == 2)drawCircle(s);}public void drawRectangle(Shape r) {System.out.println("绘制矩形");}public void drawCircle(Shape r) {System.out.println("绘制圆形");}
}
// 基类
class Shape {int m_type;
}class Rectangle extends Shape {Rectangle() {super.m_type = 1;}
}class Circle extends Shape {Circle() {super.m_type = 2;}
}
运行结果
分析
public class Ocp {public static void main(String[] args) {GraphicEditor graphicEditor = new GraphicEditor();graphicEditor.drawCircle(new Circle());graphicEditor.drawRectangle(new Rectangle());graphicEditor.drawTriangle(new Triangle()); }
}
//用于绘图的类[使用方]
class GraphicEditor {//接收不同的对象并调用相应的方法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)drawRectangle(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("绘制三角形");}
}
// 基类
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;}
}
例子改进
public class Ocp {public static void main(String[] args) {GraphicEditor graphicEditor = new GraphicEditor();graphicEditor.drawShape(new Circle());graphicEditor.drawShape(new Rectangle());graphicEditor.drawShape(new Triangle());}
}
//用于绘图的类
class GraphicEditor {//接收不同的对象并调用相应的方法public void drawShape(Shape s) {s.draw();}
}
// 基类
abstract class Shape {int m_type;public abstract void draw() ;
}class Rectangle extends Shape {Rectangle() {super.m_type = 1;}@Overridepublic void draw() {System.out.println("绘制矩形");}
}class Circle extends Shape {Circle() {super.m_type = 2;}@Overridepublic void draw() {System.out.println("绘制圆形");}
}class Triangle extends Shape {Triangle() {super.m_type = 3;}@Overridepublic void draw() {System.out.println("绘制三角形");}
}
分析
对于提供方直接新增类即可,使用方无需新增方法
Java设计模式七大原则-开闭原则相关推荐
- 设计模式之王者原则 开闭原则
设计模式之王者原则 开闭原则 为什么说 它--开闭原则是王者原则呢? 因为所有原则都是围绕它来实现的,怎么说呢,为什么会有这么多原则呢,都是为了完成它,只要实现它你的代码就不会有什么大的问题 一.定 ...
- 设计模式中的开闭原则
Table of Contents 1 设计模式中的开闭原则 1.1 基本原则 2 模式中的开-闭原则 2.1 策略模式 2.2 简单工厂 2.3 工厂方法 2.4 抽象工厂 2.5 建造者模式 2. ...
- 设计模式6大原则-开闭原则
设计模式6大原则-开闭原则 定义:一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 开闭原则理念就是当需求改变时,希望在不改变原有代码的前提下,通过扩展模块.函数来满足新需求. 开闭原则是其他 ...
- 带你认识六种设计原则(开闭原则、里氏代换原则、依赖倒转原则....)
前言 1. 设计原则 1.1. 开-闭原则 1.2. 里氏代换原则 1.3. 依赖倒转原则 1.4. 接口隔离原则 1.5. 合成/聚合原则 1.6. 迪米特法则 前言 学习设计模式之前先要了解其中的 ...
- 六大设计原则--开闭原则
定义 software entities like classes, modules and functions should be open for extension but closed for ...
- 【设计模式】软件设计七大原则 ( 开闭原则 )
文章目录 一.开闭原则简介 二.开闭原则代码示例 1.商品接口 2.普通商品类 3.折扣商品类 4.测试类 一.开闭原则简介 开闭原则 : 定义 : 一个 软件实体 , 类 / 模块 / 函数 , 对 ...
- 设计模式七大原则——开闭原则
1.什么是开闭原则? 开闭原则(Open Closed Principle)是编程中最基础.最重要的设计原则. 一个软件实体如类,模块和函数应该对扩展开放(对提供方),对修改关闭(对使用方).用抽象构 ...
- Java七大设计原则 - 开闭原则
一. 什么是开闭原则(Open Closed Principle)? 开闭原则(Open Closed Principle,OCP)又称为ocp原则, 由勃兰特·梅耶(Bertrand Meyer)提 ...
- java设计模式之设计原则①开闭原则
定义:一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 强调的是用抽象构建框架,用实现扩展细节. 优点:提高软件系统的可复用性及可维护性 例如: 创建一个Course接口 获取id方法 获取n ...
最新文章
- GitHub上最受欢迎的Android开源项目TOP20
- plsql programming 18 包
- 转帖一篇:截取密码(VC++)学习消息VC++的好处
- 什么时候必须使用UI相机? 多个相机的作用原理?
- Hibernate延迟加载
- python加密解密库openssl_OpenSSL和Python实现RSA Key公钥加密私钥解密
- Linux下判断cpu物理个数,几核,是超线程还是多核心
- css盒子模型_css的盒子模型是什么
- MySQL 浅谈NOT NULL和DEFAULT的关系
- 如何通过手机访问本地编写的html页面
- ubuntu 下sopcast的使用
- 手机浏览器上网谁最快?手机浏览器速度测评
- nginx 配置 apple-app-site-association
- python3遍历目录查找文件
- Pkg打包nodejs程序整理
- C语言计算5+55+555+5555+55555
- Redis和Mecahe的简介
- windows自带Bitlocker使用问题汇总
- 2D游戏引擎Allegro 系列教程(二) Hello world!
- IOS钉钉打开网址无法上传图片
热门文章
- CSP-S 模拟 19/10/11
- 云游三晋,中机助力|中机智库
- 金融工程对计算机的要求,金融工程专业对计算机的能力要求到底是什么?
- 蓝宝石RX590GME 8G D5白金版黑苹果免驱成功
- 大学生应知道50件事
- 终于有人把云计算、大数据和 AI 讲明白了【深度好文】
- MTCNN(一)python代码训练与运行
- Apache 流框架 Flink,Spark Streaming,Storm
- USB HOST芯片SL2.1A调试心得
- python中国大学慕课平台_计算机程序设计-Python,中国大学MOOC(慕课)答案公众号搜题...