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).
69 questions
4 votes
3 answers
3k views
What is a SOLID way for creating objects dynamically using a factory?
Description I want to create an instance of an object at runtime, however, the parameters used to create the object are not always the same. To help explain, I have created a simplified example (in ...
3 votes
2 answers
266 views
Trade-offs between applying the open-closed principle and type safety
There's lots of questions about the open-closed principle here, but none that I found satisfied this particular problem. At $job, I'm teaching my team Rust, but I believe the nature of this question ...
2 votes
6 answers
422 views
Do private members/methods inherently violate the open-closed principle?
Doesnt private members and methods inherently violate the open-closed principle? Given that private, protected and public modifiers are supported. I came across this many times in codebases where ...
8 votes
7 answers
1k views
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{ ...
2 votes
4 answers
2k views
My concrete classes only have a constructor. Everything else comes from an abstract class. Is this bad practice?
I think that I've taken the Open-Closed and Single Responsibility principles too far. Previously, I had one huge static class containing every method that has C# talk to stored procedures on my SQL ...
6 votes
5 answers
2k views
How to refactor this code to obey the ‘open-closed’ principle?
The UML is listed below. There are different products with different preferential strategies. After adding these products into the shopping cart, the caller needs to call the checkout() method to ...
-1 votes
1 answer
407 views
How to apply the Open-Closed design principle for different parameters in each class
I am a noob in design principles and design patterns, this might seem like a very silly question. Some classes in my code have the following structure (image linked): https://ibb.co/nrp1t2g As you can ...
-1 votes
1 answer
105 views
How can I cleanly express these dependencies?
Consider this hypothetical scenario. I have a user form that has a collection of controls that are made available/unavailable based on certain criteria. One important criterion is product type. On ...