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
vote
3 answers

Proper Way to Handle Item Click Recycler View

I have 5 row to display in RecyclerView. When user clicks to one of the items, another activity opens. Each item has starts different activity. I handled the click event like below. switch (getAdapterPosition()) { case 1: …
1
vote
2 answers

Open-Close principle about new features

There is something I do not understand about open-close principle. Let's say that you have done this code: public abstract class Player { public string Name { get; set; } public int Level { get; set; } } public sealed class Fighter : Player…
Maxime Recuerda
  • 476
  • 3
  • 14
1
vote
1 answer

How can I refactor this ruby code using the Open/Closed principle or Strategy pattern

How can I refactor this ruby code using the Open/Closed principle or Strategy pattern ? I know that the main thought is 'software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification' but how can I…
1
vote
1 answer

Single responsability principle vs open close principle

I'm writting a program to show a collection of questions to an user, collect his responses and print them. I have different types of questions depending on the kind of response they require: integer, boolean or text. I started writing this…
1
vote
1 answer

Crashing or ErrorValue in Python - closing files

I have a class where all of our data is in, here I open a file by: carIn= open("dataIn.txt","w") carOut= open("dataUit.txt","w") In another class I have a loop for the main program. I closed my file in the loop but it won't open again. If I close…
1
vote
2 answers

Utilizing Open Closed Principle (SOLID)

I've seen couple of samples for SOLID Open Close Principle. And those explanations are usually pretty clear. But there is still 1 more question in my mind, which is how do we initialize those different classes without using the conditional…
muhihsan
  • 2,250
  • 6
  • 27
  • 42
1
vote
0 answers

Does it violate Open Closed Principle

I have an exception handling code that i think that it violates Open/Closed Principle. Because of these exceptions derived from system.exception i can not find a way for applying Open closed principle like the classical Shape example…
1
vote
1 answer

AutoMapper : Open - Closed Principle

I got the code from the given link AutoMapper Code for Open Closed Principle Code I am trying to use it in my project but I am stuck due to Static API has been removed from AutoMapper version 4.2.0. For reference see this Please can any one help me…
C J
  • 429
  • 1
  • 10
  • 34
1
vote
3 answers

Does inheriting multiple class from a base class (DTO) breaks any of the SOLID principles?

I derive multiple DTOs (data transfer object) from a base DTO. I have a property in base DTO (isUpdateAvailable) which is shared across all derived class. I have a method which is common for multiple use case that takes the base DTO and uses it…
1
vote
1 answer

Does adding new fields (and/or methods) break OCP (open closed principle)?

Let's say I have an XML file structure to import to a database: FN 7777 Michael Smith
1
vote
0 answers

Technique for using SOLID Open/Closed Principle with enums and validation

I am searching for the best technique to use to ensure that I follow the Open/Closed Principle in the following situation. Essentially, I have inherited some code that is a nightmare to extend as it requires so many modifications: public enum Basis…
Craig Broadman
  • 185
  • 3
  • 8
1
vote
1 answer

How do you write code that conforms to the OCP?

I have recently been trying to learn about basic design principles and the OCP has me a bit confused. It makes sense that when a change happens it is preferable to extend the system rather than modify existing and working parts. But isn't this more…
Josh
  • 838
  • 3
  • 11
  • 22
1
vote
2 answers

How to add another parameter to an existing method following SOLID principle in Objective C

I have a method -(NSData*)getCommonDataWithCommandID:(int)commandID withChannelNumber:(int)channelNo withDataArray:(NSArray*)dataArray withByteArraySize:(int)byteArraySize { } This is called from a lot of places and have a big hierarchy of…
Shuvo Joseph
  • 894
  • 1
  • 12
  • 21
1
vote
4 answers

Breaking SOLID Principles in multiple implementation of an Interface

I am facing a problem with dependency inversion in a factory method and it is also breaking Open Closed principle. My code looks like below codes public interface IWriter { void WriteToStorage(string data); } public class…
1
vote
1 answer

Does passing a field in a parameter to an external method call violate the Open/Closed principle?

In the code below, does passing a private member, _field, from class Foo as an external method parameter (Bar.DoSomething(_field)) violate the Open/Closed principle in SOLID programming practices? In object-oriented programming, the open/closed…
David Vogel
  • 465
  • 8
  • 23