Example explains the OCP opening and closing principle in Java design pattern programming _java

Source: Internet
Author: User
Tags closure

Definition: A software entity such as classes, modules, and functions should be open to extensions and closed for modification.
The problem: in the lifecycle of the software, changes, upgrades, and maintenance, for reasons that require modifications to the original code of the software, may introduce errors into the old code, and may cause us to refactor the entire functionality and require that the original code be tested again.
Solution: When software needs to change, try to make changes by extending the behavior of the software entity rather than by modifying existing code. The
         closing principle is the most basic design principle in object-oriented design, which guides us to establish a stable and flexible system. The opening and closing principle may be the most obscure of the six principles of design pattern, it only tells us to open to the extension, to modify the closure, but in the end how to do to open the extension to the modification, and not explicitly tell us. In the past, if someone told me, "you must adhere to the opening and closing principles when you design", I will feel he did not say anything, but seems to have said anything. Because the opening and closing principle is really a void.
         After careful consideration and careful reading of many design patterns, finally, there is a little understanding of the principle of closure. In fact, we follow the design pattern of the top 5 principles, and the use of 23 design patterns is to follow the principle of opening and closing. That is to say, as long as we have complied with the previous 5 principles, design of the software is naturally consistent with the open and closed principle, the open and close principle is more like the front five Principles compliance level of the "average score", the first 5 principles to comply with the good, the average points naturally high, indicating that the software design and closing principles to comply with the good If the previous 5 principles are not good, then the open and closed principle is not adhered to.
         In fact, the author believes that the open and closed principle is to express the meaning of this: use the abstract to build the framework, with the implementation of extended details. Because of the flexibility of abstraction and wide adaptability, as long as the abstract reasonable, can basically maintain the stability of the software architecture. And the variable details in the software, we use the implementation class derived from the abstract to expand, when the software needs to change, we need to be based on the need to derive a implementation class to expand on it. Of course, the premise is that our abstraction should be reasonable, to be forward-looking and predictable changes in demand.
    

In the definition of the opening and closing principle, a software entity can refer to a software module, a local structure composed of multiple classes, or a separate class.
Any software needs to face a very important problem, that is, their requirements will change over time. When the software system needs to face new requirements, we should try to ensure that the system design framework is stable. If a software design conforms to the open and closed principle, it can be easily extended to the system, and no need to modify the existing code when extending, so that the software system has the flexibility and adaptability while having better stability and continuity. With the software becoming more and more large, the software life is more and more long, the software maintenance cost is more and more high, the design satisfies the opening and closing principle software system also becomes more and more important.
In order to meet the opening and closing principles, it is necessary to abstract the system design, abstraction is the key to the opening and closing principle. In Java, C # and other programming languages, you can define a relatively stable abstraction layer for the system, and move the different implementation behavior to the concrete implementation layer to complete. 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 extend it through concrete classes. If you need to modify the behavior of the system, do not need to make any changes to the abstraction layer, only need to add new specific classes to achieve new business functions can be achieved without modifying the existing code on the basis of the expansion of the function of the system to achieve the requirements of the open and closed principle.
The CRM system developed by Sunny software company can display various types of graphs, such as pie chart and histogram, and so on, in order to support a variety of graphical display, the original design is as shown in the following figure:

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

...... 
if (Type.equals ("pie")) { 
 Piechart chart = new Piechart (); 
 Chart.display (); 
} 
else if (type.equals ("Bar")) { 
 Barchart chart = new Barchart (); 
 Chart.display (); 
} 
...... 

In this code, if you need to add a new chart class, such as Line chart Linechart, you need to modify the Chartdisplay class display () method of the source code, add new judgment logic, violate the open and closed principle.
The system is reconstructed so that it conforms to the principle of opening and closing.
In this example, because the display () method in 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, so as to add new chart classes without having to modify the source code to meet the opening and closing principles. The following are specific practices:
(1) Add an abstract chart class Abstractchart, and take various concrete 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, the structure is shown in the following figure:

  in Figure 2, we introduced the abstract chart class Abstractchart, and Chartdisplay to the abstract chart class, and the Setchart () method sets the instantiated concrete chart object by the client. The display () method of the Chart object is called in Chartdisplay's display () method to show the chart. If you need to add a new chart, such as a line chart Linechart, just use Linechart as a subclass of Abstractchart, inject a Linechart object into the Chartdisplay from the client without modifying the source code of the existing class library.     
       Note: Because the configuration files in the format XML and properties are plain text files, can be edited directly through the VI editor or Notepad, and do not need to compile, so in the software development, generally do not change the configuration file is considered to the system source code changes. If a system is extended to only modify the configuration file, and the original Java code or C # code does not make any changes, the system can be considered to be a system that conforms to the opening and closing principles.

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.