Skip to main content

Command Palette

Search for a command to run...

Solid Software Design: Applying the Open/Closed Principle (OCP) in Java

Updated
3 min read
Solid Software Design: Applying the Open/Closed Principle (OCP) in Java

Definition

The Open/Closed Principle is one of the SOLID principles in software design. It states that a component should be open for extension but closed for modification. This means that we can add new functionalities without altering the existing code, improving the maintainability and scalability of the system.

What is Open-Closed Principle?. Definition of OCP, violation, benefits | by  Jun Bang | Medium

Example in Java

Below is a Java example implementing this principle using a Discount interface and different discount strategies:

interface Discount {
    double applyDiscount(double price);
}

class FixedDiscount implements Discount {
    @Override
    public double applyDiscount(double price) {
        return price - 10;
    }
}

class PercentageDiscount implements Discount {
    @Override
    public double applyDiscount(double price) {
        return price * 0.9;
    }
}

class Store {
    public double calculateFinalPrice(double price, Discount discount) {
        return discount.applyDiscount(price);
    }
}

// Usage
public class Main {
    public static void main(String[] args) {
        Store store = new Store();
        double price = 100;
        System.out.println("Price with fixed discount: " + store.calculateFinalPrice(price, new FixedDiscount()));
        System.out.println("Price with percentage discount: " + store.calculateFinalPrice(price, new PercentageDiscount()));
    }
}

Explanation

In this design:

  • The Discount interface defines a contract for different discount types.

  • FixedDiscount and PercentageDiscount implement different discount strategies.

  • The Store class uses the Discount interface to calculate the final price, allowing new discount types to be added without modifying its code.

  • In the Main class, these strategies are demonstrated without affecting the existing structure.

This approach adheres to the Open/Closed Principle because if a new discount type is required in the future, we can simply create a new class implementing Discount without altering existing classes.

Automation with GitHub Actions

To ensure that the project builds and runs correctly on every code change, a Continuous Integration (CI) workflow was implemented using GitHub Actions. This allows the Java code to be compiled and executed automatically upon every push to the main branch.

Here is the workflow configuration file (.github/workflows/java.yml):

name: Java CI

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Set up JDK
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'

      - name: Compile code
        run: javac src/*.java

      - name: Run Main class
        run: java -cp src Main

✅ This pipeline performs the following steps:

  • Checks out the code from the repository.

  • Installs Java 17 using the Temurin distribution.

  • Compiles all .java files in the src folder.

  • Executes the Main class to validate the logic.

The CI automation ensures consistent behavior across environments, simplifies collaboration, and quickly detects errors after each push.

🎥 Watch code explanation on YouTube: https://www.youtube.com/watch?v=JNSkuRmwJ3o

The full source code and workflow can be explored here:
🔗 GitHub Repository – OCP Example in Java

Comparison with SRP and DIP

PrincipleDefinitionKey Advantage
Single Responsibility Principle (SRP)A class should have only one reason to change.Simplifies debugging and maintenance by keeping each class focused on a single task.
Open/Closed Principle (OCP)A component should be open for extension but closed for modification.Enhances flexibility by allowing new features to be added without altering existing code.
Dependency Inversion Principle (DIP)High-level modules should not depend on low-level modules; both should depend on abstractions.Improves modularity and testability by reducing dependencies on concrete implementations.

These principles together help in designing flexible, maintainable, and scalable software systems.

Conclusion

The Open/Closed Principle is essential for building scalable and maintainable applications. By ensuring that components are open for extension but closed for modification, we can introduce new functionalities without altering existing code, reducing the risk of introducing bugs and making the system more adaptable to future changes. When combined with other SOLID principles like SRP and DIP, OCP contributes to a more modular and robust software design, ultimately improving development efficiency and software quality.

A

I really enjoyed this article on the open/closed principle in Java. It clearly explains how we can design classes that can be extended without modification, which is key to keeping code clean and maintainable. The practical examples help you understand how to apply this principle in real-world projects.

E

I found this article to be a clear and practical explanation of the Open/Closed Principle. The Java example made it easy to grasp how the principle works in real code, and the connection with GitHub Actions for CI added a nice touch on applying good design with modern development workflows.

E

This article perfectly exemplifies how the Open/Closed Principle (OCP) transcends being a mere "best practice" and becomes a pillar for evolutionary architectures. By implementing markdown strategies through interfaces in Java, not only does the OCP comply, but a deeper meta-principle is revealed: structural stability in software design.

E

This article clearly explains the Open/Closed Principle (OCP) and demonstrates it with a practical Java example of discount strategies. By adhering to OCP, the system allows new discount types to be added without modifying existing code, which enhances flexibility and maintainability. The inclusion of a GitHub Actions automation workflow adds an extra layer of practicality by ensuring the code is consistently tested with every change.

M

This article does an excellent job explaining the Open/Closed Principle with a clear and simple example in Java. One interesting aspect that could be discussed further is how the OCP can be applied in real-world systems that require frequent updates or additional features. For instance, in a scenario where third-party integrations need to be added regularly, OCP allows the code to be extended without impacting existing functionality. It might also be valuable to touch on potential challenges when using OCP, such as the complexity that can arise if a system becomes too modular, making it harder to manage and understand. Lastly, showcasing how tools like design patterns or frameworks support OCP in large systems could provide deeper insights into how to effectively apply this principle in production environments.

B

This article provides a clear and practical explanation of the Open/Closed Principle (OCP) and its application in Java. It highlights how OCP helps in creating scalable and maintainable software by allowing new functionalities to be added without modifying existing code. The example demonstrates the principle effectively, and the comparison with other SOLID principles like SRP and DIP adds valuable context to the discussion. Overall, it's a great resource for developers looking to improve their software design practices.