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

Is there any benefit about using Polymorphism over Enum in this scenario?

The Scenario I'm creating a dynamic query builder to send to another component (a report builder). Some parts of the query have placeholders. For example: SELECT DISTINCT ID, NAME AS VALUE FROM EVENTS WHERE {{ESTABLISHMENTFILTER.ID}} IS NULL OR…
Danilo Ruziska
  • 457
  • 2
  • 6
  • 13
3
votes
3 answers

How to respect Open closed principle when you have business logic change?

We are doing some big changes in our system and I'd like to know the best way to implement these new business logic rules, respecting SOLID principles : Open / Closed principles says "Open for extension, but close for modification" ok, but how can I…
remi bourgarel
  • 9,231
  • 4
  • 40
  • 73
3
votes
3 answers

Class Design in Game Programming

I am developing a game for a company. I will only develop this game for 2 months. My company ask me to make my code clean and extendable so they can hire another programmer when they need to add more features. I have read Clean Code by Uncle Bob. I…
3
votes
1 answer

Is it a good design to use Google Guavas Predicates to decouple filter methods from classes

Today I wrote the following article in my blog about how Google Guavas Predicates can be used to increase the extensibility of your design. This made me ask myself the following question: "Is the need for this solution caused by a deeper design…
3
votes
2 answers

Anonymous innerclass declared in an interface: what is the outerclass?

Consider the following: public class OuterClass { private String attribute = "outer"; class InnerClass { private String attribute = "inner"; public doSomething() { System.out.println(this.attribute); …
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
3
votes
4 answers

Design pattern to remove switch case

I have a requirement to verify whether the postal code for a particular country is mandatory or not based on the countryid supplied. Currently I'm doing this with a switch statement but this code breaks the Open/Closed SOLID principle. I would like…
3
votes
4 answers

Template specialization or conditional expressions?

I am deep into a new project which I address with a bunch of templates and specializations of them. Now, after a day without programming, I find myself asking whether it is really worth the extra lines of code. The question is: What are the…
steffen
  • 8,572
  • 11
  • 52
  • 90
2
votes
1 answer

Using the MVP pattern and OO principles

I'm trying to apply the principles of object-oriented programming in a scenario that uses the MVP pattern. I got 4 solutions, and the last two I liked more. However most of the solutions break down certain principles as SRP, IOC / DIP, Open-Closed…
2
votes
2 answers

Understanding the relationship between Liskov and OCP

I am solidifying my understanding of the relationship between Liskov Substitutional Principal and Open Close Principal. If anybody could confirm my deductions and answer my questions below that would be great. I have the following classes. As you…
Chris
  • 7,996
  • 11
  • 66
  • 98
2
votes
3 answers

Open-Closed Priciple: is this rephrasing correct?

I have spent a lot of time trying to understand the principle as it is stated. Read maybe several dozen articles "explaining" it, but it feels like everyone is just providing an example without actually specifying the ways that it works in our code…
Zeks
  • 2,265
  • 20
  • 32
2
votes
1 answer

Testing Open-Close principle in Java

To demonstrate Open/Closed principle of SOLID principle, I implemented the following code. Code: interface IFaculty{ void admin(); } class ITFac implements IFaculty{ @Override public void admin() { System.out.println("IT Fac…
ShamaliF
  • 25
  • 3
2
votes
1 answer

Open closed principle implementation

I am trying to refactor below code to adhere open close principle Its some bit of code extracted for question purpose but basically here calculate method behave differently based on invoice type public class Invoice { private…
user12073359
  • 289
  • 1
  • 11
2
votes
4 answers

Does the Factory Pattern in js violate the Open-Close principle?

since JS does not support abstract classes or inheritance, every time we want to add a new type to be created when using factory pattern, we will have to modify the code which mean we violate the open-close principle. for example, in the snapshot…
2
votes
1 answer

Does adding new attributes of an Entity and columns database break OCP (open closed principle)?

If I have to add a new database column, and by consequence, a new attribute to an Entity, it gonna break the OCP principle? The government changes the law and we need to persist a new attribute to the database. It's ok about modifying the…
2
votes
1 answer

Open Closed Principle C#: Is Private Set + Constructor Init an Example of it?

My Workmate believes that the following code is an example of the Open Closed Principle in C#: public class MyClass { public int Id { get; private set; } public int Count{ get; private set; } public int Maximum{ get; private set; } …
fourbeatcoder
  • 1,159
  • 3
  • 13
  • 21