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
0 answers

What causes RTTI to violate the Open-Closed Principle?

For your reference, the Open-Closed Principle (OCP): https://www.cs.utexas.edu/users/downing/papers/OCP.pdf So I am almost done with my OOP course and I have a question about the OCP and why RTTI violates it. I have an idea, but I just want to make…
Zach
  • 4,555
  • 9
  • 31
  • 52
1
vote
1 answer

How to load Func from configuration file - Factory pattern - Open Closed principal

Below is my factory class: public class BulkFactory { private BulkFactory() { } static readonly Dictionary> _dict = new Dictionary>(); public static T Create(string id) { Func
Vipul
  • 1,563
  • 4
  • 22
  • 46
1
vote
1 answer

What's the most appropriate way to apply the Open-Closed Principle in this app using C#?

Scenario Each night we perform a series of calculations on about a million customer contracts. Each contract is related to one fo a group of about ten products, each of which may employ variations on the set of calculation for be performed (although…
Mike Woodhouse
  • 51,832
  • 12
  • 88
  • 127
1
vote
2 answers

Writing tests without violating SRP, OCP, DRY

I am trying to understand these three principles better. My question is... How do I write tests without violating SRP, OCP, and DRY? My current design violates DRY because of the similar code in the test files. I can't merge the test files…
1
vote
1 answer

OOP - Can and should events be part of the abstractions?

Imagine that I have a system of objects that are event emitters and also can listen to events in other objects. In this system these objects communicate between them mainly using events. I want to follow good object-oriented practices an I'm…
user2279235
1
vote
1 answer

Does C# re-abstracting violate the open/closed principle?

As a Java programmer (beginner) introducing myself to C#, I have found you can re-abstract an already implemented method like this (code from this answer) public class D { public virtual void DoWork(int i) { // Original…
Blueriver
  • 3,212
  • 3
  • 16
  • 33
1
vote
0 answers

Is "file" also participate in open closed principle?

I know according to open closed principle, something like class,interface,code.... should be unchanged when add new functions or contents, but is file also in this case? I was designing a game, the game have some monster information, and the game…
ggrr
  • 7,737
  • 5
  • 31
  • 53
1
vote
4 answers

If else statement inside a for loop[java]

I have some code like this, converting Sting to objects. But it includes if statement inside for loop, to indicate the first node and last node. Is there any better coding than this? splitQueue = "Man-1/Man-2/Man-3/Man-4".split("/"); QueueSplitLen =…
Jerry Z.
  • 2,031
  • 3
  • 22
  • 28
1
vote
2 answers

OCP Java SE 6 Practice Questions - WeatherTest Enum

A question in "OCP Java SE 6 Programmer Practice Exams (Exam 310-065)" Assesment test 2. Given: public class WeatherTest { static Weather w; public static void main(String[] args) { System.out.print(w.RAINY.count + " " + w.Sunny.count + "…
Syed Siraj Uddin
  • 583
  • 1
  • 5
  • 13
1
vote
0 answers

Open/Closed principle and enterprise WCF services

I'm in the process of Developing and Maintaining a set of WCF services. The functionality is grouped in the services. Payment stuff goes in one service, pure Data in another, and application functions (mostly CRUD) in the third. About every release…
Chloe
  • 483
  • 4
  • 14
1
vote
1 answer

Java,design pattern:Manager for Use cases and Actors

In a project I have 3 Actors (user,expert,admin) and 5 main Use cases(CRUD: create,read,update,delete and Sync). But access of users to each use case differs from other Actors.For example user can create one entity,but expert and admin can create…
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
1
vote
1 answer

Android XML internal storage

I am trying to save some data using XML in android to save into the devices internal storage. I want to be sure the app is written by the open/closed and single responsibility -principle. (So i could easly switch between methods of…
1
vote
2 answers

Issues with the Open/Closed Principle?

Was reading up on the Open/Closed principle of SOLID design and was curious about it's maintainability. Lets say I have child class B and C which inherit from parent class A. B has methods unique to B, C has methods unique to C. Parent class A has…
1
vote
2 answers

How can I avoid violation of Open Closed with method parameters?

In the code below the MessageProcessor class is violating Open Closed principle - every new IMessage implementation requires a change to this class. Is there a nice clean pattern for this kind of scenario that doesn't violate O/C? public interface…
1
vote
4 answers

open closed principle - refactoring to create base class based on new features

So when original code was written there was only a need for say LabTest class. But now say we have new requirements to add say RadiologyTest, EKGTest etc. These classes have a lot in common hence it makes sense to have a base class. But that will…
krprasad
  • 359
  • 1
  • 4
  • 17