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

In open close principle, what exactly is modification and extension?

Ok, so I know the obvious example of modification, that is when we need to modify an existing behaviour to add a new one. But, when we just need to add code that doesn't change anything but just add new functionality, does this count as modification…
vacih86456
  • 237
  • 1
  • 7
-1
votes
2 answers

3 Layers architecture in vending machine, while keeping OCP

I'm working on a vending machine project, and i tried to split it into UI and BL layers, But I'm getting into problem. For example I have this function for paying in coins, which derives from an abstract class: public override void Pay(decimal…
-1
votes
1 answer

Is there "more" real world example of Abstract Factory pattern?

I am currently going through the design patterns of OPP. Just to give you some background. This is not my first time encountering the patterns. I've been programming for a while (decade or so) and I am pretty familiar with many programming…
-1
votes
1 answer

How to create a factory object without violating OCP in Typescript

I just learn about a Factory Pattern and want to implement that in Typescript. I check many sites like this and notice that all of the examples violating OCP because they have to use many if statements to find a suitable subclass constructor. I also…
-1
votes
1 answer

Open and close lightbox

I make a lightbox, but I have a little problem. I can close the lightbox when I click on the X button, but I want to close the lightbox when I click outside the box too. And the second isuee, is when I have on open lightbox and I click on another.…
Mikki
  • 1
  • 1
-1
votes
2 answers

Interface instance by id

We have different type of images, we store the image on disk in subfolders accordingly, and the metadata in database, including the fileTypeId. currently we have this: public enum FileTypes { Document=1, ProfileProto ... } and switch…
barii
  • 343
  • 1
  • 3
  • 12
-2
votes
1 answer

Error when casting a reference parameter with dynamic_cast in C++

I'm studying the book Agile Software Development by Robert C. Martin. In an example on the Open-Closed Principle I had a problem with the dynamic_cast <>. the example is as follows: #include #include #include using…
emacos
  • 541
  • 1
  • 8
  • 17
-2
votes
8 answers

If I have a full unit test suite for an application, must I still apply the Open/Closed Principle (OCP)?

The Wikipedia article on OCP says (emphasis mine): ... the open/closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"... This is especially valuable in a production…
Rogério
  • 16,171
  • 2
  • 50
  • 63
1 2 3
16
17