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
6
votes
1 answer

Simple Factory vs Factory Method: Switch statement in factory vs. client

I understand that one of the main advantages of the Factory Method over Simple Factory is that it doesn't violate the Open-Closed SOLID Principle. That is, the former doesn't require modifying the switch statement when new types are added. There is…
6
votes
3 answers

Extending List and Violating The Open/Closed Principle

I just created the following method in one of my classes public static bool Assimilate(this List first, List second) { // Trivial if (first.Count == 0 || second.Count == 0) { return false; } // Sort the…
Trauer
  • 1,981
  • 2
  • 18
  • 40
5
votes
2 answers

Factory design pattern and violation of OCP (Open-Closed Principle)

The factory in this tutorial obviously violates the OCP. Every time a shape is added to the system, we need to add it in the factory to support it. I'm thinking of another implementation and I'd like to know if there are any drawbacks. public class…
Youssef13
  • 3,836
  • 3
  • 24
  • 41
5
votes
2 answers

Do checked exceptions violate the open closed principle?

I have two checked exceptions: TestException1 and TestException2 and the following code: void p1() throws TestException1{ p2(); } void p2() throws TestException1 { p3(); } void p3() throws TestException1 {} Does the editing of the…
Overflow 404
  • 482
  • 5
  • 20
5
votes
4 answers

SOLID - are the Single Responsibility Principle and the Open/Closed Principle mutually exclusive?

The Single Responsibility Principle states that: A class should have one, and only one, reason to change. The Open/Closed Principle states that: You should be able to extend a classes behavior, without modifying it. How can a developer respect…
5
votes
1 answer

Are there any benefits to following the open/closed principle when using BDD?

The open/closed principle seems to be about preventing regressions in an object or method. Given that your code is covered by tests because you're practicing BDD this seems a redundant requirement. In addition it seems to introduce additional…
opsb
  • 29,325
  • 19
  • 89
  • 99
5
votes
1 answer

Open closed principle

I understand that this principle states that a module is open for extension but closed for modification, and this is clear when we want to upgrade/modify a method in some class - we create another class that inherits the base class and then override…
Ivan
  • 1,081
  • 2
  • 17
  • 43
5
votes
1 answer

Do I understand Open-Closed Principle right?

Let's say in first version of my hypothetical software I have a simple class like this: public Class Version1 { public void Method1() { Console.WriteLine("Hello"); } } In second version, I have an upgrade that requires the Method…
Ivan
  • 1,081
  • 2
  • 17
  • 43
5
votes
1 answer

Am I violating the "open/closed" principle?

Scenario: I stored some information (e.g. an array of doubles) in a class field (say field Measurements, array of integers in a class MeasureData). Now I would like to use this data to perform some calculations (e.g compute the arithmetic mean of…
Gabriele
  • 469
  • 4
  • 10
5
votes
2 answers

Why do static and instance init blocks in Enums behave differently from those in Classes

In studying for the Java certification test I learned that static initialization blocks run once when the class is loaded, in order of appearance within the source code, that instance initialization blocks run each time an instance is created, and…
paniclater
  • 903
  • 1
  • 12
  • 18
5
votes
1 answer

Method overriding: same argument list types (or COMPATIBLE types)?

In the book I use to prepare for the new Oracle Certified Professional - Java SE7 Programmer exam, in the section that deals with method overriding, I have come across the following: The overriding method should have the same argument list types…
Peter Perháč
  • 20,434
  • 21
  • 120
  • 152
4
votes
1 answer

WCF Derived types and violation of the Open/Closed principle

I have a base class that I use in WCF service calls, [KnownType(typeof(MyDerivedClass))] public abstract class MyBaseClass { //some properties } I derive from it and every time I derive I have to add the [KnownType(typeof(MyDerivedClass))]…
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
4
votes
4 answers

Exceptions and errors report order

What rules applies to the following code: try { assert (false) : "jane"; } catch (Exception e2) { System.out.print("ae2 "); } finally { throw new IllegalArgumentException(); } Assetions are enabled. Why…
mmatloka
  • 1,986
  • 1
  • 20
  • 46
4
votes
3 answers

How to apply open-closed principle when creating objects

I'm busy parsing xml documents (google docs api) and putting individual documents into objects. There are different types of documents (documents, spreadsheets, presentations). Most information about these documents is the same, but some is…
Ikke
  • 99,403
  • 23
  • 97
  • 120
4
votes
1 answer

How does this example violate LSP, which then causes violation of OCP?

From Agile Principles, Patterns, and Practices in C# by Robert Martin, Listing 10-1. A violation of LSP causing a violation of OCP struct Point {double x, y;} public enum ShapeType {square, circle}; public class …
1 2
3
16 17