The application of open and closed principles of design patterns in Java programming is resolved

  • 2020-04-01 04:38:59
  • OfStack

The Open Closed Principle is one of the most fundamental design principles in the Java world, and it guides how to build a stable, flexible system.

Definition:

A software entity such as a class, module, or function should be open for extension and closed for modification.

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

The open close principle means that a software entity should change by extension, not by modifying existing code.

The software entity consists of the following parts:

A module in a project or software product that is divided according to certain logical rules Abstract and class methods

The open close principle is a principle that constrains the current development design for the future of the software entity.

Note: the open and close principle is open to extension and closed to modification, which does not mean that no modification is made. The change of the low-level module necessarily requires the coupling of the high-level module, or it is an isolated and meaningless code fragment.


Type of change:

Logical change Submodule variation Visible attempt to change

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


The importance of the open and close principle:

1. The impact of open and close principle on testing
The open close principle is to keep the original test code still working, we just need to test the extended code.

2. The open and close principle can improve reusability
In object-oriented design, all logic is composed from atomic logic, rather than implementing a single business logic in a single class. Only in this way can the code be reused, and the smaller the granularity, the more likely it is to be reused.

3. The open and close principle can improve maintainability
Requirements for object-oriented development.
 

How to use the open close principle:

1. Abstract constraints
First, the boundary of the extension is limited by the interface or abstract class. Public methods that do not exist in the interface or abstract class are not allowed.

Second, try to use interfaces or abstract classes instead of implementation classes for parameter types and reference objects.

Third, the abstraction layer should be kept as stable as possible, and should not be modified once determined.

2. Metadata controls the behavior of the module
Metadata is the data used to describe the environment and data. It is colloquially referred to as configuration parameters, which can be obtained from a file or a database.

The Spring container is a typical example of metadata Control module behavior, culminating in Inversion of Control

3. Develop the project charter
In a team, it is important to have a project charter, because the charter specifies a contract that all people must follow, and for the project, the contract is better than the configuration.

4. Encapsulate change
Encapsulation of change has two meanings:

First, encapsulate the same changes in an interface or abstract class.

Second, wrap different changes into different interfaces or abstract classes, and you shouldn't have two different changes in the same interface or abstract class.

example

Here's an example, first of all 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 learn about the GraphicEditor class, then add new types in our drawShape, and then add functions. Here's 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 know the drawing logic, you put the implementation in a subclass.

Conclusion:
1. Compliance with the open and close principle can improve the scalability and maintainability of software.
2. Most of the design patterns and design principles are in the implementation of the open and closed principle.


Related articles: