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
1
vote
3 answers

Command Pattern Implementation DRY breaks Single Responsibility Principle & Open Closed Principle

I'm currently implementing the Command-Handler Pattern for a service I'm designing where the Command is essentially a DTO for the Handler's .Handle() method. As I begin to implement various concrete classes I realize that in order to satisfy the…
1
vote
2 answers

Adding a method to the middle of a class hierarchy respecting O/C principle

I have a class hierarchy responsible for parsing or mapping one model (or message) to another. It has non-trivial logic. internal interface IMessageParser where T : class where K : class { K Serialize(T originalMessage); T…
1
vote
1 answer

How to respect the open/close principle with primitive parameters?

is it a good pattern to add a parameter object instead of primitive for respecting the Open Close Principle. Lets say I have this interface public interface IBar{ void DoSomething(int id); } If one day I need to add a parameter, I'll have to…
remi bourgarel
  • 9,231
  • 4
  • 40
  • 73
1
vote
1 answer

Does an API based on inheritance violate OCP? Can this be achieved with a provider model/dependency injection?

I'm designing an API for the first time, and trying to follow SOLID guidelines. One of the things I find myself struggling with is balancing OCP and testability with simplicity and ease of extensibility. This open-source API is geared toward…
0
votes
1 answer

Making Validation Generic

I have the following C# code. Here the validations are kept outside the class to satisfy Open – Closed Principle. This is working fine. But the challenge is – the validations are not generic. It is specific to employee class (E.g…
LCJ
  • 22,196
  • 67
  • 260
  • 418
0
votes
1 answer

How to refactor this code so that it conforms to Open-Close principle?

The question is actually derived from this link. Suppose I have a problem like this: A book-shop buys and sells two types of books: (1) Non-technical {Title, Author, Price} (2) Technical {Title, Author, Price, CD} Also, customer gets a CD when he…
user366312
  • 16,949
  • 65
  • 235
  • 452
0
votes
0 answers

deploying application to Openshift using jenkins DSL using private repositories

HiIs there any way to perform these: Any ways is approved as long as it creates a container image and registers it in the Open shift private container registry. Uses Jenkins DSL to deploy to Open Shift In the Jenkins pipeline, ROKS team should…
anonymous
  • 21
  • 3
0
votes
3 answers

Open closed principle implementation python

I have case like based on a type send email to different platform (this is sample use case) So I have done below so far from abc import ABC, abstractclassmethod import json class Email(ABC): @abstractclassmethod def sendEmail(self): …
user12073359
  • 289
  • 1
  • 11
0
votes
3 answers

Understanding the Open/Closed principle - do we have a counter-example

I'm trying to reach a better understanding of the Open/Closed principle. I'm familiar with reference material such as Robert Martin's explanation and Jon Skeet's exploration of the ideas, and the related concept of Protected Variation. I have a…
djna
  • 54,992
  • 14
  • 74
  • 117
0
votes
0 answers

IBM Cloud Pak for data Red Hat OpenShift installation problems

What I am doing wrong? Have: ocp 4.10.38 sefl managed. Trying to install Cloud Pak 4.6.3 using cpd-cli regarding instructions. Done all preparation tasks, generate new Entitlement key, exported it as var, and successfully ran cpd-cli manage…
0
votes
1 answer

Best Practice Open Close Principle

Please take a look at my class. In it WoW avatars are created. In the constructor method instances of different classes are created. If the program is extended by a new class (e.g. Tauren), the constructor method must be changed stupidly. One would…
0
votes
1 answer

I know we can't call an extension method with the same name. But what are the other ways to achieve this?

I know we can't call an extension method with the same name. But what are the other ways to achieve this? I want to call an extension method which I created to replace an another api method(sealed) which is used by dozen's of classes so making a…
0
votes
1 answer

Workbook_Open() execution for hidding / showing Sheets at Excel startup

I have an application with two sheets. The First is a LOGIN sheet where users can login to have access to the second sheet : the Working Sheet. At the start of the application, i have the Working Sheet set to xlSheetHidden in the sub…
0
votes
1 answer

Open/Closed Principle - How to integrate the principle with this code?

I'm a beginner in desing pattern and I wanna to refactor some of my old codes for practice. The code below is basically a JSON schema validator, I retrieve the schemas (RestoreJsonSchemas method) through a folder with .txt files, containing the…
fuba
  • 23
  • 5
0
votes
0 answers

Open-closed principle vs extensible XML models

I'd like to create a model to represent math equations in XML. But I can't figure out how to without violating the open-closed principle. For example:
Matt Thomas
  • 5,279
  • 4
  • 27
  • 59