Daily learning - Java design principles (Day4) - opening and closing principles

Posted by rolwong on Wed, 16 Feb 2022 04:21:50 +0100

We must put forward such tasks for ourselves: first, learning, second, learning, and third, learning.
There is never a shortcut to learning. You can reach the peak step by step.


Some of the notes come from the video teaching of design mode in Silicon Valley.

1, Basic introduction

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

Sometimes in the development process, we need to provide interfaces to third parties. We belong to the provider, so the third party belongs to the user. If the user wants to use our interface, they have to follow our specifications, and they are not allowed to change it at will. If one company changes, the others may not work.

2, Application examples

1. Example 1

Advantages and disadvantages of the following examples:

  1. The advantages are easy to understand, simple and easy to operate.
  2. The disadvantage is that it violates the ocp (opening and closing) principle of the design pattern, that is, it is open to expansion (provider) and closed to modification (user). That is, when we add new functions to the class, we try not to modify the code, or modify the code as little as possible

For example, if we want to add a new type of triangle, we need to make the following modifications. There are many modifications

public class Ocp {

    public static void main(String[] args) {
        //Use to see the existing problems
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
    }

}

//This is a class for drawing [user]
class GraphicEditor {
    //Receive the Shape object, and then draw different shapes according to the 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);
    }

    //draw rectangle
    public void drawRectangle(Shape r) {
        System.out.println(" draw rectangle ");
    }

    //Draw circle
    public void drawCircle(Shape r) {
        System.out.println(" Draw circle ");
    }

    //Draw triangle
    public void drawTriangle(Shape r) {
        System.out.println(" Draw triangle ");
    }
}

//Shape class, base class
class Shape {
    int m_type;
}

class Rectangle extends Shape {
    Rectangle() {
        super.m_type = 1;
    }
}

class Circle extends Shape {
    Circle() {
        super.m_type = 2;
    }
}

//Add draw triangle
class Triangle extends Shape {
    Triangle() {
        super.m_type = 3;
    }
}

2. Example 2

Analysis of improvement ideas:

Make the Shape class created into an abstract class and provide an abstract draw method for subclasses to implement,
In this way, when we have a new type of graphics, we only need to make the new graphics class inherit Shape and implement the draw method. The user's code does not need to be repaired - > meets the opening and closing principle

public class Ocp {

    public static void main(String[] args) {
        //Use to see the existing problems
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
        graphicEditor.drawShape(new OtherGraphic());
    }

}

//This is a class for drawing [user]
class GraphicEditor {
    //Receive the Shape object and call the draw method
    public void drawShape(Shape s) {
        s.draw();
    }
}

//Shape class, base class
abstract class Shape {
    int m_type;

    public abstract void draw();//Abstract method
}

class Rectangle extends Shape {
    Rectangle() {
        super.m_type = 1;
    }

    @Override
    public void draw() {
        // TODO Auto-generated method stub
        System.out.println(" draw rectangle ");
    }
}

class Circle extends Shape {
    Circle() {
        super.m_type = 2;
    }

    @Override
    public void draw() {
        // TODO Auto-generated method stub
        System.out.println(" Draw circle ");
    }
}

//Add draw triangle
class Triangle extends Shape {
    Triangle() {
        super.m_type = 3;
    }

    @Override
    public void draw() {
        // TODO Auto-generated method stub
        System.out.println(" Draw triangle ");
    }
}

//Add a new graphic
class OtherGraphic extends Shape {
    OtherGraphic() {
        super.m_type = 4;
    }

    @Override
    public void draw() {
        // TODO Auto-generated method stub
        System.out.println(" Draw other graphics ");
    }
}

Topics: Java Design Pattern