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
9
votes
4 answers

Does overriding violate the Open/Closed principle?

The open/closed principle states that a class should be open for extension but closed for modification. I thought that the modification part referred strictly to altering the source code of the base class. But I had an argument with someone saying…
conectionist
  • 2,694
  • 6
  • 28
  • 50
8
votes
4 answers

Open / Closed Principle - How to deal with this Switch?

I have been looking into the open closed principle and it sounds good and so I wanted to exercise it's teachings. I looked at applying my new found knowledge to an existing project and have become a little stuck right away. If a new UserType comes…
4imble
  • 13,979
  • 15
  • 70
  • 125
8
votes
3 answers

Do enums violate open/closed principle Java?

In java, If we are using an enum and eventually we want to add/remove an attribute to/from that enum, hence its usages, we are violating open/closed principle in solid principles. If so, what is the better usage of enums?
Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
7
votes
9 answers

OO Design, open/closed principle question

I've been thinking about this object oriented design question for a while now and have unable to come up with a satisfactory solution, so thought I'd throw it open to the crowds here for some opinions. I have a Game class that represents a turn…
JonC
  • 809
  • 8
  • 18
7
votes
1 answer

LARAVEL: How to use Open Close Principle of SOLID principles?

I have a following structure to use Open Close Principle class Payment{ //this is not a model class // according to OC principle this class should not focus on the implementation private $paymentInterface; public function…
Afraz Ahmad
  • 5,193
  • 28
  • 38
7
votes
2 answers

Open Closed Principle Vs Default Implementation

Java 8 introduced the concept of default implementation for interfaces? Isn't this violating Open Closed Principle, since based on the example on https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html, it seems like you can always…
7
votes
3 answers

Abstract methods and the Open-Closed principle

Suppose I have the following contrived code: abstract class Root { public abstract void PrintHierarchy(); } class Level1 : Root { override public void PrintHierarchy() { Console.WriteLine("Level1 is a child of Root"); } } class Level2…
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
7
votes
2 answers

Using IoC and Dependency Injection, how to wrap code with a new layer of implementation without violating the Open-Closed principle?

I'm trying to figure out how this would be done in practice, so as not to violate the Open Closed principle. Say I have a class called HttpFileDownloader that has one function that takes a url and downloads a file returning the html as a string. …
7
votes
2 answers

Getting past Open-Closed Principle

I have a simple program which draws geometrical figures based on mouse data provided by user. I've got one class which handles the mouse tracking (it gets the List with mouse movement history) and one abstract class called Shape. From this class I…
Kamil T
  • 2,232
  • 1
  • 19
  • 26
7
votes
5 answers

How can I have a behavior-rich domain entity that adheres to Open-Closed Principle?

The Open-Closed Principle states: software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification I'm designing a domain right now and including quite a bit of behavior in my domain entities. I'm…
6
votes
1 answer

What is the difference between the "Open/closed principle" and the "Dependency inversion principle"?

I read articles about S.O.L.I.D. but I don't see any difference between OCP and DIP. Look at this example of OCP: http://www.oodesign.com/open-close-principle.html The code which holds OCP also fulfils the DIP. Can anyone give me an example of…
6
votes
2 answers

How do Test-Driven Development and the Open/Closed Principle work together?

I've been reading up on unit testing, TDD, and the SOLID principles and I need some clarification. My understanding is that if one adheres to the open/closed principle, unit testing could become largely unnecessary due to the fact that the code is…
joelmdev
  • 11,083
  • 10
  • 65
  • 89
6
votes
3 answers

Good examples of OCP in open source libraries

There has been a lot of discussion on the subject of “Open Closed Principle” on stackoverflow. It seems however, that generally a more relaxed interpretation of the principle is prevalent, so for example the Eclipse is open for modification through…
Dan
  • 11,077
  • 20
  • 84
  • 119
6
votes
1 answer

Sealed classes in Java 17, and the open-closed principle

For the first time in a LTS version of Java (Java 17) we have the sealed keyword that, in a nutshell, give us the possibility to restrict the hierarchy: public abstract sealed class Person permits Employee, Manager { //... } If I want to…
SGiux
  • 619
  • 3
  • 10
  • 34
6
votes
2 answers

Reduce code duplication without subclass inheritance

I'm playing around with subclassing vs Interfaces and composition. I end up getting confused about a few things when it comes to the code duplication. As we all know there are alot of scenarios where subclassing and inheritance is just not the way…
1
2
3
16 17