Questions tagged [open-closed-principle]

For questions about the Open-Closed Principle of object-oriented design, coined by Bertrand Meyer in his book: Object-Oriented Software Construction. It states that, "Modules should be both open and closed." (2nd Edition, page 57)

Bertrand Meyer first published the Open-Closed Principle in the 1988 edition of his book: Object-Oriented Software Construction. He later refined it in the 1997 second edition. The principle was also adopted by Robert Martin as the second of his .

In Meyer's words (2nd Edition, page 57)

The contradiction between the two terms is only apparent as they correspond to goals of a different nature:

  • A module is said to be open if it is still available for extension. For example, it should be possible to expand its set of operations or add fields to its data structures.
  • A module is said to be closed if it is available for use by other modules. This assumes that the module has been given a well-defined, stable description (its interface in the sense of information hiding).

Meyer's solution to this contradiction was inheritance: extending a module without modifying it. Martin's solution to the OCP is a plugin architecture: inverting all dependencies to point at the system rather than away from it.

248 questions
60
votes
14 answers

What is the meaning and reasoning behind the Open/Closed Principle?

The Open/Closed Principle states that software entities (classes, modules, etc.) should be open for extension, but closed for modification. What does this mean, and why is it an important principle of good object-oriented design?
36
votes
5 answers

Configuring Automapper in Bootstrapper violates Open-Closed Principle?

I am configuring Automapper in the Bootstrapper and I call the Bootstrap() in the Application_Start(), and I've been told that this is wrong because I have to modify my Bootstrapper class each time I have to add a new mapping, so I am violating the…
Omu
  • 69,856
  • 92
  • 277
  • 407
30
votes
3 answers

How does the Visitor Pattern not violate the Open/Closed Principle?

From Wikipedia : The idea was that once completed, the implementation of a class could only be modified to correct errors; new or changed features would require that a different class be created. That class could reuse coding from the original…
25
votes
7 answers

Open-closed principle and Java "final" modifier

The open-closed principle states that "Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification". However, Joshua Bloch in his famous book "Effective Java" gives the following advice: "Design…
rmaruszewski
  • 2,407
  • 1
  • 21
  • 21
22
votes
7 answers

Clean code for removing switch condition(using polymorphism)

As SOLID principles say, it's better to remove switch conditions by converting them to classes and interfaces. I want to do it with this code: Note: This code is not real code and I just put my idea into it. MessageModel message =…
Siamak Ferdos
  • 3,181
  • 5
  • 29
  • 56
19
votes
5 answers

What is difference between the Open/Closed Principle and the Dependency Inversion Principle?

The DIP states: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. And the OCP states: Software entities…
17
votes
2 answers

Visitor Pattern + Open/Closed Principle

Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle, but still be able to add new visitable classes? The Open/Closed Principle states that "software entities (classes, modules, functions, etc.) should be open for…
dalle
  • 18,057
  • 5
  • 57
  • 81
17
votes
6 answers

Is the Open/Closed Principle a good idea?

This question is not about what OCP is. And I am not looking for simplistic answers, either. So, here is why I ask this. OCP was first described in the late 80s. It reflects the thinking and context of that time. The concern was that changing source…
Rogério
  • 16,171
  • 2
  • 50
  • 63
15
votes
6 answers

Avoiding If Else conditions

I want to refactor the following code to avoid if...else so that I don't have to change the method every time a new survey type comes in (Open/closed principle). Following is the piece of code I am considering to refactor: if (surveyType ==…
fahmi
  • 591
  • 6
  • 27
13
votes
3 answers

Does the "Open for extension, closed for modification" principle make any sense?

It looks to me like Bob Martin needed something starting with O to make SOLID and found in some old book this (possibly useless) Open/Closed principle. How can Open/Closed co-exists with the Single Responsibility, that states a class should have a…
12
votes
1 answer

Understanding the Open Closed Principle

I was refactoring some old code of a simple script file parser when I came across the following code: StringReader reader = new StringReader(scriptTextToProcess); StringBuilder scope = new StringBuilder(); string line = reader.ReadLine(); while…
Reyhn
  • 997
  • 1
  • 11
  • 22
12
votes
3 answers

Open / Closed principle - How to call the new versions?

I'm trying to grasp the Open/Closed principle (in my case for PHP, but that shouln't really make a difference). The way i understand it is that a class is never open for modification. Only for bug fixing. If i wanted to add new code to the class,…
w00
  • 26,172
  • 30
  • 101
  • 147
11
votes
3 answers

Does the Factory Method pattern violate the Open/Closed principle?

Does the Factory Method pattern (not to be confused with the Factory or Abstract Factory patterns) violate the Open/Closed principle? Update: To clarify, I'm referring to the scenario where a concrete class has static factory methods on it. For…
Todd Ropog
  • 1,133
  • 1
  • 14
  • 23
10
votes
2 answers

DAO pattern and the Open-Closed Principle

I've seen and worked with a lot of older, JDBC-based DAO code that usually start out with CRUD methods. My question relates specifically to the retrieval methods, or 'finders'. Typically what I find is that the DAOs start out with two…
user620884
10
votes
5 answers

extending parameterized factory method in java

I'm new to OOP and learning design patterns so I wrote some simple code to try out a Factory Method, and all seems well, except when I want to add another sub-type. Here's the code so far: public interface Person { public String…
1
2 3
16 17