文章目录
前言
一、基本介绍
开闭原则(OCP)是编程中最基础、最重要的设计原则
一个软件实体,如类、模块和函数应该对提供方扩展开放,对使用方修改关闭。用抽象构建框架,用实现扩展细节
当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化
编程中遵循其它的原则,以及使用设计模式的目的就是遵循开闭原则
二、代码类图设计
类图设计如下:
三、代码对比
代码一:
public class One {
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();
else if (s.m_type == 2)
drawCircle();
else if (s.m_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 m_type;
}
class Rectangle extends Shape {
public Rectangle() {
super.m_type = 1;
}
}
class Circle extends Shape {
public Circle() {
super.m_type = 2;
}
}
//新增画三角形
class Triangle extends Shape {
public Triangle() {
super.m_type = 3;
}
}
代码二:
public class Two {
public static void main(String[] args) {
GraphicEditor1 graphicEditor1 = new GraphicEditor1();
graphicEditor1.drawShape(new Rectangle1());
graphicEditor1.drawShape(new Circle1());
graphicEditor1.drawShape(new Triangle1());
graphicEditor1.drawShape(new Others());
}
}
//这是一个用于绘图的类 [使用方]
class GraphicEditor1 {
public void drawShape(Shape1 shape1){
shape1.draw();
}
}
abstract class Shape1 {
public abstract void draw();
}
class Rectangle1 extends Shape1 {
public void draw() {
System.out.println("绘制矩形");
}
}
class Circle1 extends Shape1 {
public void draw() {
System.out.println("绘制圆形");
}
}
//新增画三角形
class Triangle1 extends Shape1 {
public void draw() {
System.out.println("绘制三角形");
}
}
class Others extends Shape1{
public void draw() {
System.out.println("绘制其它图形");
}
}