Questions tagged [open-closed-principle]
40 questions
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
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,073
- 7
- 38
- 58
9
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…

user270386
- 223
- 1
- 5
8
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…

Joeri Hendrickx
- 388
- 2
- 6
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…

Mihai Bratulescu
- 181
- 3
7
votes
2 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()…

Standback
- 1,310
- 2
- 9
- 14
6
votes
5 answers
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 calculate the totalPrice and loyaltyPoints according…

Abner
- 71
- 7
6
votes
5 answers
Are "open-closed principle" and "less coupling" rationales to use global state?
Consider I'm writing a mobile app with user-login feature, using a framework which can be simplified like that:
class UserData{
static token="";
static name="";
static balance=0;
}
class Main{
constructor(){
…

aacceeggiikk
- 707
- 1
- 5
- 6
5
votes
4 answers
Properly re-design from Switch to Polymorphism (Open/Close principle)
I'm having major troubles trying to get rid of this switch statement. To put a little of context first, I'm working with asynchronous batch operations. These operations can be applied to any entity in the system. You just have to send a different…

Christopher Francisco
- 2,392
- 1
- 12
- 27
5
votes
2 answers
Specification pattern and open closed principle
I'm studying the SOLID principles and I'm having some troubles dealing with the Specification Pattern and the open/closed principle.
The fact is that the Specification pattern introduced by Eric Evans and Martin Fowlers creates some abstraction and…

mfrachet
- 1,481
- 3
- 15
- 21
5
votes
7 answers
Is it appropriate to not follow the O/C principle if you have unit test coverage?
I find it more convenient to 'modify'existing classes rather than extending them. Thus, I violate the open closed principle of 'not modifying the compiled and tested source code, instead extending the functionality'. I feel comfortable in modifying…

Anmol Gupta
- 175
- 3
5
votes
3 answers
Command pattern and open-closed-principle
Does the command pattern uses OCP ?
In a command patter the invoker is only extensible by actually extending the class. If we want to add custom methods to it, we can make our own sub-class or we could modify the base class's constructor, which…

Liusara
- 67
- 2
4
votes
5 answers
Open Close Principle is applicable when requirement is to add new operation(s) to existing type?
I understand OCP in SOLID can be applied when we have same set of interfaces defined via abstract type, so that we can have varying implementations of those interfaces via concrete types.
For example, I have my business logic working with…

rahulaga-msft
- 1,402
- 1
- 11
- 24
3
votes
5 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…

wcminipgasker2023
- 721
- 1
- 3
- 10
3
votes
4 answers
Relationship between inheritance and single responsibility principle
Example 1:
Let me assume that I have a base class A. Class B extends Class A (Class B is a derived class).
Can I conclude, Class B doesn't obey single responsibility principle since it uses the concept of inheritance? I.e certain changes happen to…

Dharma Teja Bandaru
- 65
- 1