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

Nested abstractions and Open-Closed Principle

I am about to implement a document generator. I am insisting on following the Open-Closed Principle, what brings me some trouble. The requirements are as follows: there will be multiple document types (i.e. agreement, the power of attorney) there…
Dawid
  • 763
  • 1
  • 8
  • 17
0
votes
3 answers

How to apply registry pattern to make "select class depend on input" obey open closed principle?

for example, I have some Fruits: Fruit.h #ifndef __Fruit__ #define __Fruit__ #include class Fruit{ public: virtual void hi(std::string username)=0; }; #endif Apple.h #include "Fruit.h" #include class Apple : public…
ggrr
  • 7,737
  • 5
  • 31
  • 53
0
votes
0 answers

Ordering items when conforming to the open-closed principle

I have a scenario where I need to return a list of actions and represent them on a context menu. The software has a standard list of actions, e.g: Analyse Design Develop Implement I am tasked with extending the original software for a different…
amarsha4
  • 453
  • 6
  • 21
0
votes
1 answer

Open/Closed principle and violation of encapsulation

Could you please check if the following code is correct or not? The fact is that I found something similar in propduction code and I have doubt if it matches Open/Closed principle. public abstract class CustomClass { private…
aime
  • 247
  • 4
  • 16
0
votes
1 answer

Open / Closed Principle & Single Responsibilty -- Graphs

I am coding a Graph exploration program and have hit a bit of a stumbling block. My graph is made up of Vertex and NetworkLink objects, and can be obatined by querying a GeographyModel object. The idea is List is retrieved from the…
0
votes
1 answer

Set properties values with violating open-close principle validation

I have a ServiceField class which contains FieldId and Value as two properties . public class ServiceField { public int FieldId { get; set; } public string Value { get; set; } } ///
shujaat siddiqui
  • 1,527
  • 1
  • 20
  • 41
0
votes
1 answer

Is it possible in Ruby to redefine a initialize method?

I have a third-party code that's like this: class Foo def initialize @hello = "world" end def msg @hello end end Then, I added a new file foo_redefinition.rb with these contents class Foo def initialize @hello = "welt" …
Luís Guilherme
  • 2,620
  • 6
  • 26
  • 41
0
votes
0 answers

Golang - net.Conn infinite loop on the same message

I'm pretty news in Golang and only use sockets in this langage for 2days. However, I'm not sure to understand something. I know in C, I used select() to know who wrote etc, but here, no one is writing until one send a message. After this message…
Emixam23
  • 3,854
  • 8
  • 50
  • 107
0
votes
4 answers

Does a class with has-a relationship with a Collection object have a relationship with the Collection's elements?

I am studying for the Java 8 Programmer II certification, and I'm going over the relationships classes can have. I understand composition, but I wondered if there is relationship considered between the elements of a collection that is a class…
Shoikana
  • 595
  • 4
  • 8
0
votes
1 answer

Java open closed principle design

I am preparing for exam in object oriented modelling and design and can't figure out this problem. The design is in violation of open-closed principle; you can't add more JButtons without modifying the class. Redo the design so that this becomes…
user2155599
  • 23
  • 1
  • 5
0
votes
2 answers

Factory Pattern, open closed principle, interfaces and generics

I've tried to refactor some code based on the Open-Closed Principle, but I just don't seem to be able to get the following classes right when it comes to applying design patterns. (I apologize for the many classes outlined below - I have reduced…
0
votes
2 answers

What is the difference between the ISP and the OCP?

I don't understand what the difference is between the Interface Segregation Principle and the Open/Closed Principle. What I understand is that the ISP must make everything depend on interfaces and the OCP on classes and I see that both of them can…
0
votes
0 answers

Scala - Does pattern matching break the Open-Closed principle?

First of all, I know this question has been asked previously here, but it wasn't clear for me. Pattern matching is used to make a function react to different types of data. One would say that if my Pattern Matching case has 4 cases and one month…
0
votes
1 answer

Scalable solution to intelligent operators for unit classes

I run this open-source library cleverly named the Unit Class Library. It's purpose, like many others is to allow for more intelligent handling of units (rounding, conversion, etc.) Here is an example of the Library at work Distance distance = new…
jth41
  • 3,808
  • 9
  • 59
  • 109
0
votes
1 answer

Processing multiple collections based on the same base type

I have a series of collections of types, all of which are derived from the same base class, and a set of predicates for searching for each one , e.g. public abstract class Animal { ... } public class Dog : Animal { ... } public class Cat…
Gavin
  • 2,321
  • 3
  • 19
  • 21