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

Objective C Category vs open/closed principle

Accorinding to the Open/Closed principle . . . software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification So can I say that the Category is a strong violation to this principle?
Howard
  • 19,215
  • 35
  • 112
  • 184
2
votes
3 answers

Dealing with inheritance hierarchy because of Open-Closed Principle

When I try to follow the Open-Closed Principle (OCP), after number of implemented use cases I always end up with hierarchy of inherited classes. Usually it happens with ViewModels in MVVM structure, because they are being changed a lot. For example…
2
votes
1 answer

Achieve Open-Closed Principle (SOLID) Using Interface

I have multiple types of payment options (Stripe, Paypal, PayUMoney etc.). I want to create seperate class for each payment type and an Payment Interface to be implemented by those classes like this, interface PaymentInterface { public function…
2
votes
1 answer

SOLID - Violated Open-Closed principle

I have the following code snippet class Vehicle{ public String brand; public double price; public int productionYear; public String toString(String formatType) { switch(formatType) { case "JSON": // JSON formatting here return…
Vlad Danila
  • 1,222
  • 3
  • 18
  • 38
2
votes
3 answers

Is there an OCP-friendly way to dynamically choose which collection in a DbContext to access?

I'm working on a method to retrieve a collection of records from a database. The records are stored in separate tables based on an aspect of the data they contain. Suppose it looks like this. public class EnglishPhrase : IPhrase { public string…
JAF
  • 385
  • 1
  • 2
  • 12
2
votes
1 answer

Inject a call automatically

I have a scenario which is based on the Open Closed principle. I have a Cart class, which has a method CalculateTotal(). This method takes MODE and QTY as parameters. Based on the MODE, the amount is calculated. public class Cart { ICalculator…
Ranjith Varatharajan
  • 1,596
  • 1
  • 33
  • 76
2
votes
1 answer

How to apply State pattern and strategy pattern together?

I need to implement in Java an application that plays and record videos. The video player has two states, Playing mode and recording mode. While on playing mode, we can Play, Pause,Stop, ForwardandBackward the video. On the other hand, in recording…
2
votes
4 answers

Strategy Pattern and Open-Closed Principle Conflict

I was reading through strategy pattern and was trying to implement it but I have got stuck at deciding the strategy implementation which I feel violates the open-closed principle. In strategy pattern we code to interface and based on client…
2
votes
3 answers

Elegant way to query a dictionary in C#

I am trying to create an elegant and extensible way of querying a dictionary which maps an enum to a set of strings. So I have this class SearchFragments that has the dictionary in it. I then want a method wherein consumers of this class can simply…
Thomas Cook
  • 4,371
  • 2
  • 25
  • 42
2
votes
1 answer

How `open/closed principle` can help avoiding to redeploy assemblies?

I've recently read a nice, easy to understand article about open/closed principle. And I've been wondered by the following experience: In a real world scenario where the code base is ten, a hundred or a thousand times larger and modifying the class…
StepUp
  • 36,391
  • 15
  • 88
  • 148
2
votes
1 answer

Open-closed principle with constructor

Learning 'SOLID' principle I'm wondering is it ok to modify a constructor if I need to add some more extension to class, eg. business logic. From what I've learned it looks like modifying constructor I violate the 'open-closed' principle, but what…
user3127896
  • 6,323
  • 15
  • 39
  • 65
2
votes
1 answer

Open-Close Principle Example

I want to apply the Open-close principle on this following example by re-writing this code. class MyQueue extends ArrayList implements Queue { int front=0, back=0; MyQueue() { } … void put(E e) { add(back++, e); ...} E get ()…
user2639830
2
votes
2 answers

Application of open/closed principle in Java

I try to understand the SOLID principles and therefore implemented some java snippets. My concern is the OCP at the moment. Having following samples, public abstract class Bakery { public abstract Bakegood…
Arthur Eirich
  • 3,368
  • 9
  • 31
  • 63
2
votes
2 answers

Observer Design Pattern Issues

I am working on a large project in C++ that will have a graphical user interface. The user interface will use some design pattern (MVVM/MVC) that will rely on the observer pattern. My problem is that I currently have no way of predicting which…
2
votes
1 answer

Adding new commands with Command Design Pattern

I'm having a dilemma regarding Command Design Pattern. Receiver is the interface for a class that knows how to perform operations. In the example case given in the link, receiver class Stock Trade knows how to perform 2 operations, StockTrade#buy…
ioreskovic
  • 5,531
  • 5
  • 39
  • 70