java design patterns study notes - the principle of opening and closing

basic introduction

1, the shutter (OCP) when the most basic programming principle, the most important design principles
2, a software entity, such as classes, and functions should be open to block extension but closed for modification. Abstract framework for building, implemented with extension detail. That is to provide an open square, close to the consumer .
3, when the software needs to change, try to achieve by extending the behavior class software entity change, rather than to achieve change by modifying existing code
4, other programming to follow the principles and purposes of design patterns is to follow the opening and closing principle .

First to show a piece of code

public class Ocp {
    public static void main(String[] args) {
        // 使用,看看存在的问题
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawCircle(new Shape());
        graphicEditor.drawRectangle(new Shape());
        graphicEditor.drawOther(new Shape());

    }
}

//这是一个用于绘图的类
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) { 
            // 需在使用方添加(else if)代码快
            drawOther(s);
        }
    }

    public void drawRectangle(Shape s) {
        System.out.println("这是矩形");
    }

    public void drawCircle(Shape s) {
        System.out.println("这是圆形");
    }

    // 需在使用方添加新的方法
    public void drawOther(Shape s) {
        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 Other extends Shape {
    Other() {
        super.m_type = 3;
    }
}

Analysis of the problems in this code

1, the code is easy to understand, clear thinking.
2, but in violation of the design patterns ocp principle that open for extension, but closed for modification . That is, when we add new features to the class, try not to modify the code, modify the code or less as possible.
3, each additional features need to be added (else if) code for fast, too much (else if) the consumer makes the code is too bloated, operating efficiency is not high.

Ways to Improve

Create a Shape class, and provides an abstract draw () method, so that subclasses implement the method. Whenever adding a graphic kind, so that the figure type inherits Shape class and implement the draw () method. Thus, a consumer simply write drawShape method, a pattern class of the incoming object, to use the corresponding mapping method. Only need to modify the code provider, consumers do not need to modify the code, following the principle of ocp

Use ocp principle

public class Ocp {
    public static void main(String[] args) {
        // 遵循ocp原则
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Other());

    }
}

//这是一个用于绘图的类,[使用方]
class GraphicEditor {
    // 接收Shape对象,调用其对应的draw方法
    public void drawShape(Shape s) {
        s.draw();
    }
}

//Shape类,基类
abstract class Shape {
    public int m_type;

    public abstract void draw(); // 抽象方法
}

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

    @Override
    public void draw() {
        System.out.println("这是矩形");
    }
}

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

    @Override
    public void draw() {
        System.out.println("这是圆形");
    }
}

//新增一个其他图形
class Other extends Shape {
    Other() {
        super.m_type = 3;
    }

    @Override
    public void draw() {
        System.out.println("这是其他图形");
    }
}

Guess you like

Origin www.cnblogs.com/windowsxpxp/p/12177770.html