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
1 answer

Does this break the Open/Closed Principle?

I have spent most of my afternoon reading on the Open/Closed Principle, and I can't seem to understand it fully. Here are some referred articles I have read already and it seems as I missed something. Understanding the Open Closed Principle The end…
Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
2
votes
1 answer

Dispatching on type vs polymorphism in C#. The business entities and non-business logic

I want to perform some operations (db persistence, json serialization, etc.) on an object, based on its type. Polymorphism is the usual way to do this, but I don't want to add a lot of non-business logic to my models. Here is a simple type…
2
votes
1 answer

Open closed principle, refactoring

I'm trying to apply OCP to a code snippet I have that in it's current state is really smelly, but I feel I'm not getting all the way to the end. Current code: public abstract class SomeObject {} public class SpecificObject1 : SomeObject {} public…
Marcus
  • 1,866
  • 1
  • 20
  • 33
2
votes
1 answer

Does Façade leverage the Open-Closed Principle?

The Wikipedia page (as of today 2013-02-27) for the Open-Closed Principle says that it's realized via inheritance. The name Open/Closed Principle has been used in two ways. Both ways use inheritance to resolve the apparent dilemma, but the goals,…
Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
2
votes
2 answers

Implementing Visitor using InstanceOf

I do well master Visitor Pattern. However, I wonder something. Most important motivation to use Visitor Pattern is to add logic involvingo specific Data Models in client side without needing to check for the real data object type. The technique used…
Mik378
  • 21,881
  • 15
  • 82
  • 180
2
votes
6 answers

How to satisfy Open Closed Principle in Factory Pattern using Reflection?

I am trying to learn Object Oriented Design pattern using Head First Design Pattern. Here is one example of factory pattern from the book, where i want to add new pizza item without violating the open closed principle. In the given example code…
2
votes
1 answer

Ways to Improve this unit of work class, related to open/closed principle and dependency injection / inversion of control

I am interesting in looking at ways that I could improve the use of the below UnitOfWork class. As you can see it currently doesn't have a UnitOfWork interface so when I am using this in my MVC controllers I have to create a new object which makes…
2
votes
1 answer

Call DAL Method Based on Type

I'm working on an app where I need to call one of two data methods based on the generic type of the calling class. For example, if T is of type Foo, I'll call data.GetFoo(): private static List GetObjectList(DateTime…
Bob Horn
  • 33,387
  • 34
  • 113
  • 219
1
vote
3 answers

How to implement the Open-Closed Principle in error handling for new error types?

In the given JavaScript code snippet, there are two functions - handleClientError and handleServerError - that handle client-side and server-side errors, respectively. The handleError function is used to determine which error type needs to be…
1
vote
3 answers

How can I make type environments for an expression to satisfy specific conditions?

I have a question to make the environment of if true then x else y not closed, closed but not well typed, and closed and well typed. How can I do this in OCaml? All I know is that not closed means that there is a variable that is not bound. So x and…
1
vote
1 answer

Open-Closed Principle in Python OOD

I am designing a parking lot. Now I have three kinds of parking spots. I want to initialize my parking lot like this: Building({Size.SMALL: 5, Size.MEDIUM: 6, Size.LARGE: 7}) This means this parking lot has 5 small spots, 6 medium spots and 7…
1
vote
3 answers

How do I implement the open-close principle?

My intention is to describe the architecture of a tennis court management software with a class diagram, keeping the open-closed principle. Initially, the software should only provide the following functions: CourtOfAWeek(week:int):void. This…
1
vote
1 answer

Is the service affected when all masters are stopped?

Is the service affected when all masters are stopped? OpenShift 4 Infra Node 3 Master Node 3 Worker Node 3 ※ Router pods are in the Infra Node. The work request is as follows. frontend(DC) -> api(DC) Internet -> Infra Node(Router) -> SDN ->…
ddakker
  • 55
  • 7
1
vote
1 answer

How to achieve Open closed principle in correct way?

I have a class and inside that, I have a method which is responsible for process Template based on the certain condition public doProcessing(@RequestParam("tempId") int TempId){ if(tempId == 1){ //some logic } elseIf(tempId == 2){ // another type of…
Beginner
  • 145
  • 7
1
vote
0 answers

OCP : Issue with Multi-container POD with SRIOV interfaces

Environment : Openshift Container Platform - version 4.7 Pod description : Number of containers per pod - 3 (For simplicity, lets name it as A, B, C) Number of interfaces per pod - 3 (with the help of Multus -…