Application of the open and closed principle of design pattern in Java programming _java

Source: Internet
Author: User

Open Closed principle is the most fundamental design principle in the Java world, and it guides us in building a stable and flexible system.

Defined:

A software entity such as classes, modules, and functions should be open to extensions and closed for modification.

Softeware entities like classes,modules and functions should is open for extension but closed for modifications.

The opening and closing principle means that a software entity should implement changes by extension rather than by modifying existing code.

The software entity includes the following sections:

    • A module in a project or software product that is divided according to certain logical rules
    • Abstractions and classes
    • Method

The opening and closing principle is a principle for the future of the software entity to restrain the current development design.

Note: The opening and closing principle to expand Open, to modify the closure, does not mean that no changes, low-level module changes, there must be a high level module to be coupled, otherwise it is an isolated meaningless code fragment.


Types of changes:

    • Logical change
    • Sub-module changes
    • Visible trying to change

The basic path of a project should be this: project development, refactoring, testing, commissioning, operation, the reconstruction of which can modify the original design and code, operation and maintenance to minimize the original code modification, maintain the purity of historical code, improve the stability of the system.


the importance of the closing principle:

1. The effect of opening and closing principles on testing
The open and closed principle but keep the original test code can still work, we only need to test the extended code.

2. The principle of opening and closing can improve the reusability
In object-oriented design, all logic is a combination of atomic logic, rather than a separate implementation of a business logic in a class. Only in this way can the code be reused, the smaller the granularity, the greater the likelihood of being reused.

3. Opening and closing principle can improve maintainability
Object-oriented development requirements.

How to use the closing principle:

1. Abstract constraints
First, the extension is bounded by an interface or an abstract class constraint, and a public method that does not exist in an interface or abstract class is not allowed;

Second, parameter types, reference objects use interfaces or abstract classes as much as possible, rather than implementing classes;

Third, the abstraction layer remains as stable as possible, and modification is not allowed once it is determined.

2. Meta-data (metadata) control module behavior
Metadata is used to describe the environment and data, in layman's terms, configuration parameters, parameters can be obtained from the file, or from the database.

The spring container is an example of the behavior of a typical metadata control module, in which the ultimate is control reversal (inversion of controls)

3. Development of the Project Charter
In a team, it is important to establish a project Charter, as the Charter specifies the conventions that all personnel must follow, and for the project, the agreement is better than the configuration.

4. Package Change
The encapsulation of change contains two meanings:

First, the same changes are encapsulated into an interface or abstract class;

Second, the different changes are encapsulated into different interfaces or abstract classes, and there should not be two different changes appearing in the same interface or abstract class.

Example

Here's an example of a bad example:

Class Graphiceditor {public 
  void Drawshape (Shape s) { 
    if (s.m_type==1) 
      DrawRectangle (s); 
    else if (s.m_type==2) 
      drawcircle (s); 
  } 
  public void Drawcircle (Circle r) {...} 
  public void DrawRectangle (Rectangle r) {...}} 
  
 Class Shape { 
  int m_type; 
 } 
  
 Class Rectangle extends Shape { 
  Rectangle () { 
    super.m_type=1; 
  } 
 } 
  
 Class Circle extends Shape { 
  Circle () { 
    super.m_type=2; 
  } 
 }  

When we want to extend a shape, we need to understand the Graphiceditor class, then add the new type to the Drawshape, and then add the function. Here is the improved code:

Class Graphiceditor {public 
  void Drawshape (Shape s) { 
    s.draw () 
  
 }} Class Shape { 
  abstract void Draw (); 
 } 
  
 Class Rectangle extends Shape {public 
  void Draw () { 
    //Draw the Rectangle 
  } 
 }  


You don't need to understand the drawing logic, you put the implementation into subclasses.

Summary:
1. Adhering to the opening and closing principles can improve software scalability and maintainability.
2. Most design patterns and design principles are in the implementation of the opening and closing principle.

Related Article

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.