Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Open/closed principle (OCP)

, 23 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Classes should be easy to extend and alter without changing their code.

SOLID principles:

Classes should be easy to extend and alter without changing their code.

Let's consider the calculator example once again:

class CalculatingMachine {
  public static void add(int x, int y) {
    System.out.println(x + y);
  }
}

How do I change its behavior? What if want it to use something else but System.out for printing? Looks like OCP is violated here, there's no explicit way I can do it. One of the possible solutions here is to make this class abstract.

(not for production, illustrative purposes only)

abstract class AbstractCalculatingMachine {
  public void add(int x, int y) { // cant use static anymore
    getPrintStream().print(x + y);
  }
  
  protected abstract PrintStream getPrintStream();
}

class ConsoleCalculatingMachine extends AbstractCalculatingMachine {
  @Override
  protected PrintStream getPrintStream() {
    return System.out;
  }
}

I have all the calculator machine-specific behavior implemented in abstract class AbstractCalculatingMachine. I also have ConsoleCalculatingMachine that uses System.out for printing the results. As soon as I need a way to print results to file, I'll just subclass AbstractCalculatingMachine once again and will return file-based PrintStream in getPrintStream(). This can be done without altering AbstractCalculatingMachine class' code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

loki2302
Software Developer (Senior)
Russian Federation Russian Federation
No Biography provided
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.141022.1 | Last Updated 23 Jul 2012
Article Copyright 2012 by loki2302
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid