Questions tagged [open-closed-principle]

The OCP is often introduced as "Software entities [...] should be open for extension, but closed for modification" (see detailed tag info for references).

Questions about the Open-Closed-Principle, as it was described by several authors mentioned in the Wikipedia article.

This is one of the SOLID principles.

69 questions
63
votes
8 answers

LSP vs OCP / Liskov Substitution VS Open Close

I am trying to understand the SOLID principles of OOP and I've come to the conclusion that LSP and OCP have some similarities (if not to say more). the open/closed principle states "software entities (classes, modules, functions, etc.) should be…
38
votes
8 answers

Is overriding Object.finalize() really bad?

The main two arguments against overriding Object.finalize() is that: You don't get to decide when it's called. It may not get called at all. If I understand this correctly, I don't think those are good enough reasons to hate Object.finalize() so…
user138956
20
votes
1 answer

Why does(/did) Bertrand Meyer think subclassing is the only way to extend a "closed" module?

In Meyer's Object-Oriented Software Construction (1988) he defines the open/closed principle as follows: A module will be said to be open if it is still available for extension. For example, it should be possible to add fields to the data…
18
votes
8 answers

Is it ok to inherit a class without adding anything to the child, to respect the Open Closed principle?

To clarify the question, here is my context (or something very similar). I have an interface, that I call IDataSource. The implementing classes contain information to retrieve data. So I have multiple classes implementing it, let's say FileSource,…
Kilazur
  • 305
  • 2
  • 5
14
votes
4 answers

Do mocks violate the Open/Closed principle?

Some time ago I read, on a Stack Overflow answer that I can't find, a sentence that explained that you should test public APIs, and the author said that you should test interfaces. The author also explained that if a method implementation changed,…
12
votes
2 answers

Does TDD contradict the open-closed principle?

My understanding of the TDD methodology is that (failing) test cases are written promptly after finalizing the requirements. My understanding of the open-closed principle (in the context of OOP) is to structure the class hierarchy so that any new…
Vorac
  • 7,169
12
votes
7 answers

Refactoring and Open / Closed principle

I have recently being reading a web site about clean code development (I do not put a link here because it is not in English). One of the principles advertised by this site is the Open Closed Principle: each software component should be open for…
12
votes
5 answers

Is overloading an example of the Open/closed principle?

Wikipedia says "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification" The word functions caught my eyes, and I now wonder if we can assume that creating an overload for a method can be…
Saeed Neamati
  • 18,318
10
votes
2 answers

Liskov's substitution principle : If subtype has some extra behaviour implemented, which is not present in type, then is this violation of LSP?

In my quest to write better, cleaner code, I am learning about SOLID principles. In this, LSP is proving to be little difficult to grasp properly. My doubt is what if I have some extra methods in my subtype, S, which were not there in type, T, will…
10
votes
2 answers

Static factory method in base class

An increasingly popular definition of factory method is: a static method of a class that returns an object of that class' type. But unlike a constructor, the actual object it returns might be an instance of a subclass. From :…
9
votes
5 answers

How do I make this Open/Closed example also obey Single-Responsibility?

This is a simple example, but it reflects a tension between SOLID principles that I often find myself struggling with. A popular example of the Open/Closed Principle (e.g. [1], [2]) imagines that you have many Shape classes, and a drawShape()…
9
votes
4 answers

In DDD, how do I persist an aggregate containing polymorphism

I've been developing applications according to the principles of DDD for a while now, and, as many, I often run into issues when it comes to persisting an aggregate. One of the main advantages of DDD is that it allows me to use the full power of OO…
8
votes
7 answers

Is "avoid feature envy" violating "open closed principle"?

After reading What is a" feature envy" code and why is it considered a code smell?" I know the following code is suffering from "feature envy": public class Garden{ public float width; public float height; } public…
8
votes
5 answers

Open Closed principle in design patterns

I am bit confused about how Open Closed principle can be applied in real life. Requirement in any business changes over the time. According to Open-Closed principle you should extend the class instead modifying the existing class. To me every time…
parag
  • 181
7
votes
2 answers

Difficulty making this class open-closed

Here is my problem: I want to read input from different HID devices such as a gamepad, racing well, joystick, etc. Pretty much any game controller. The issue is that they all have different inputs. The gamepad has buttons, switches and sticks while…
1
2 3 4 5