Open and close principle of object-oriented design principle

Source: Internet
Author: User

The opening and closing principle is the first cornerstone of object-oriented reusable design, and it is the most important object-oriented design principle. The opening and closing principle was proposed by Bertrand Meyer in 1988 and is defined as follows:

Opening and closing principle (open-closed Principle, OCP): A software entity should open for expansion and close for modifications. That is, the software entity should try to extend without modifying the original code.

In the definition of open and closed principle, a software entity can refer to a software module, a local structure consisting of multiple classes, or a separate class .

Any software needs to face a very important problem, that is, their needs will change over time. When the software system needs to face the new requirements, we should try to ensure that the system design framework is stable. If a software design conforms to the open and close principle, it is very convenient to extend the system, and without modifying the existing code in the extension, the software system has the adaptability and flexibility to have good stability and continuity. With the increasing of software scale, the longer life of software and the higher cost of software maintenance, it becomes more and more important to design a software system that satisfies the opening and shutting principle.

In order to satisfy the principle of opening and closing, abstract design is needed for the system, and abstraction is the key to open and close principle. In programming languages such as Java, C #, you can define a relatively stable abstraction layer for the system, and move the different implementation behaviors to the specific implementation layer. In many object-oriented programming languages, interfaces, abstract classes and other mechanisms are provided, which can be used to define the abstraction layer of the system and then extend it through specific classes. If you need to modify the behavior of the system, there is no need to make any changes to the abstraction layer, just add a new concrete class to implement the new business functions, realize the function of extending the system without modifying the existing code, and achieve the requirement of the open and closed principle.

The CRM system developed by Sunny software companies can display various types of charts, such as pie charts and histograms, in order to support a variety of chart display methods, as shown in the original design Scenario 1:

Figure 1 Schematic diagram of the initial design scheme

The following code snippet exists in the display () method of the Chartdisplay class:

[Java]View PlainCopy
    1. ......
    2. if (Type.equals ("pie")) {
    3. Piechart chart = new Piechart ();
    4. Chart.display ();
    5. }
    6. else if (type.equals ("Bar")) {
    7. Barchart chart = new Barchart ();
    8. Chart.display ();
    9. }
    10. ......

In this code, if you need to add a new chart class, such as a line chart Linechart, you need to modify the source code of the display () method of the Chartdisplay class, add new judgment logic, and violate the open and closed principle.

The system is reconstructed to conform to the opening and shutting principle.

In this example, because the display () method of the Chartdisplay class is programmed for each chart class, adding a new chart class has to modify the source code. The system can be reconstructed in an abstract way to add new chart classes without modifying the source code and meeting the open and closed principle. The following are the specific practices:

(1) Add an abstract chart class Abstractchart, the various specific chart classes as their subclasses;

(2) The Chartdisplay class is programmed for abstract chart classes, and the client determines which specific chart to use.

After refactoring, structure 2 shows:

Fig. 2 Structure diagram after reconstruction

In Figure 2, we introduced the abstract chart class Abstractchart, and Chartdisplay for the abstract chart class, and the Setchart () method is set by the client to instantiate the specific chart object, in the Chartdisplay display () The display () method in the call chart object in the method displays the chart. If you need to add a new chart, such as a line chart Linechart, you just need to add linechart as a subclass of Abstractchart, and inject a Linechart object into the Chartdisplay on the client without modifying the source code of the existing class library.

Note: Because the XML and properties format configuration file is a plain text file, can be edited directly through the VI editor or Notepad, and do not need to compile, so in software development, the configuration file is generally not modified as the system source code modification. If a system only involves modifying the configuration file when it is extended, and the original Java code or C # code does not make any changes, the system can be considered as a system that conforms to the open and closed principle.

Open and close principle of object-oriented design principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.