Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [open-closed-principle]

The tag has no usage guidance.

1
vote
1answer
57 views

What is a good way of satisfying open/closed principle in this case

I have a class that looks like this: class MyClass # takes an array of actions def initialize(actions) @actions = actions end def act @actions.each {|a| a.do} end end class ...
1
vote
1answer
97 views

Are “I'm writing android applications” and “follow open-closed principles” reasons to allow global state?

For example, suppose I have an android application, in simple, there are 3 activities : MainActivity -> Main2Activity -> Main3Activity ,which user can visit them in sequence (eg: by button ...
4
votes
5answers
202 views

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,...
0
votes
3answers
85 views

How to “open interfaces to expansion” without breaking existing implementations?

For example, I have an interface Requests that basically lists all the requests a client can make to a server program. Now an obvious problem arises where I have to add a new request to the program i....
3
votes
2answers
115 views

Clarification about an example of the polymorphic open/closed principle

My question is regarding the polymorphic Open-Closed Principle. Say I have the following client: public class Client { private Server server; public Client(Server server) { this....
1
vote
2answers
236 views

Liskov Substitution Principle

I am trying to wrap my mind around a Liskov Substitution Principle and I came across this article. SOLID Class Design: The Liskov Substitution Principle In this article, the author gives an example ...
8
votes
2answers
369 views

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. ...
1
vote
1answer
70 views

Ensure encapsulation and Open/Closed principle with Command pattern?

I'm developing a small multiplayer game. It'll be served by one websockets server and consumed by multiple consumers. As such I need to be cautious about concurrency errors. The general software ...
7
votes
2answers
780 views

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 ...
2
votes
2answers
129 views

Is replacing the call to mutual exclusive methods by an injected strategy overengineering?

I recently gave advice to refactor some code along the lines of class Validator<T> : IValidator { public ValidationResult Validate(Validatable thingToValidate) { if(typeof(T)....
3
votes
1answer
101 views

Should files/data follow rules like 'open-closed principle'?

Does 'open-closed principle' also includes files and data of the app? According to https://softwareengineering.stackexchange.com/a/296955/248528, it says files and data are't included, but it doesn't ...
3
votes
3answers
936 views

Following Open Closed Principle

I try to write "clean code" for most of the time. But practically find it very hard, meaning - gradually business requirements changes dramatically or the business requirement which seems like just a ...
0
votes
4answers
321 views

The Open/Closed Principle, how does it work for adding entities?

I'm not even sure how to ask this, but here goes: Let's say I have a Web application that incorporates Teachers, Courses and Students in such a way that I can do things like this (pseudocode): ...
5
votes
4answers
735 views

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 ...
-2
votes
2answers
211 views

Change in constructor parameter or ways to decompose the constructor? [duplicate]

Class Book { private int year; private String session; private int volume; private int number; private String khand; private Date proceeding_date; private int pageNo; Book(year,session,...
2
votes
3answers
277 views

If an enum is used without bitwise operation, and all of its values are predefined individually, is it violating open closed principle?

For example, if I use an enum, which all values are manually defined individually, for example: public enum MyNum{ Zero(0), One(1), Two(2); private final int value; MyNum(int ...
7
votes
1answer
490 views

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 ...
6
votes
4answers
2k views

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 ...
5
votes
2answers
359 views

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 ...
2
votes
1answer
609 views

Open/Closed principle, good practices, and business rules

I'm discovering craftsmanship and trying to learn it, and I decided first to understand how to work with the SOLID principles. I'm actually facing some troubles while dealing with the Open/Closed ...
2
votes
2answers
158 views

Can “do something depend on input” obey open closed principle?

For example, I have something need to do depend on user input: test.cpp #include <iostream> #include <fstream> int main(){ std::string input; std::cin >> std::noskipws >&...
5
votes
7answers
707 views

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 ...
4
votes
3answers
509 views

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 ...
1
vote
4answers
418 views

Understanding Bob Martin's Closed For Modification Explanation

“Closed for modification.” Extending the behavior of a module does not result in changes to the source or binary code of the module. The binary executable version of the module, whether in a ...