Design Patterns: Open and Closed Principles (Detailed Explanation)

Design Pattern (3): Open and Closed Principle



foreword

This blogger will use CSDN to record the experience and knowledge he has personally gained and learned on the way to study software development. Interested friends can pay attention to the blogger! Perhaps a person can go fast alone, but a group of people can go farther!

1. Introduction

  1. The Open Closed Principle is the most basic and important design principle in programming

  2. A software entity such as classes, modules and functions should be open for extension (for the provider) and closed for modification (for the consumer). Build frameworks with abstractions and extend details with implementations.

  3. When the software needs to change, try to implement the change by extending the behavior of the software entity instead of modifying the existing code.

  4. Other principles are followed in programming, and the purpose of using design patterns is to follow the principle of opening and closing .

2. Code demo

1. Version 1

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;
	}
}


Advantages and disadvantages:

  1. The advantage is that it is easy to understand and easy to operate.

  2. The disadvantage is that it violates the ocp principle of the design pattern, that is, it is open to extension (provider) and closed to modification (consumer) . That is, when we add new functions to the class, try not to modify the code, or modify the code as little as possible.

  3. For example, if we want to add a new graphics type triangle at this time, we need to make the following modifications, and there are many places to modify.

  4. Improved thought analysis

    Idea: Make the created Shape class an abstract class, and provide an abstract draw method for subclasses to implement. In this way, when we have a new graphic type, we only need to let the new graphic class inherit Shape and implement the draw method can,

    The code of the user does not need to be modified -> the principle of opening and closing is satisfied

2. Version 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());
		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() {
     
     
		// TODO Auto-generated method stub
		System.out.println(" 绘制矩形 ");
	}
}

class Circle extends Shape {
     
     
	Circle() {
     
     
		super.m_type = 2;
	}
	@Override
	public void draw() {
     
     
		// TODO Auto-generated method stub
		System.out.println(" 绘制圆形 ");
	}
}

//新增画三角形
class Triangle extends Shape {
     
     
	Triangle() {
     
     
		super.m_type = 3;
	}
	@Override
	public void draw() {
     
     
		// TODO Auto-generated method stub
		System.out.println(" 绘制三角形 ");
	}
}

//新增一个图形
class OtherGraphic extends Shape {
     
     
	OtherGraphic() {
     
     
		super.m_type = 4;
	}

	@Override
	public void draw() {
     
     
		// TODO Auto-generated method stub
		System.out.println(" 绘制其它图形 ");
	}
}

Guess you like

Origin blog.csdn.net/weixin_52533007/article/details/130396369