The principle of opening and closing (when adding new functions, it will not affect the original way of use) Use abstraction to construct the framework, use practice to expand the details

basic concepts

  1. The Open Closed Principle is the most basic and most important design principle in programming
  2.  A software entity such as class, module and function should be open for extension (for the provider, the tool class is the callee ), and closed for modification (for the user, the caller ). Build the framework with abstraction, and expand the details with implementation.
  3. When the software needs to change, try to achieve the change by extending the behavior of the software entity, rather than by modifying the existing code.
  4. Follow other principles in programming, and the purpose of using design patterns is to follow the principle of opening and closing.

Things to do with the open and closed principle

Abstract the new functions that need to be added, and increase its extended functions through abstract classes, so that the user's code will not be changed. Users always implement new functions through abstract method calls of abstract classes, which were originally used Do not directly call the implementation method

 

The original function is to draw rectangles and circles through the GraphicEditor class, the following code: The design principle is to assign a value to Shape.m_type through the construction method, and draw the corresponding graphics through this value

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

	//绘制矩形
	public void drawRectangle(Shape r) {
		System.out.println(" 绘制矩形 ");
	}

	//绘制圆形
	public void drawCircle(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;
	}
}

If you need to add a new feature, draw a triangle 

First, you need to add a new class, and at the same time you need to modify the methods in the (user) GraphicEditor class

//新增画三角形
class Triangle extends Shape {
	Triangle() {
		super.m_type = 3;
	}
}

At the same time, it needs to be modified in the drawShape method, adding

else if (s.m_type == 3)
   drawTriangle(s);

And you have to add the drawTriangle() method to complete the drawing

You need to modify the original function when new functions are found,

 

ways to improve


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/qq_41813208/article/details/103077719