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

Open close principle and Collective ownership. Extreme Programming

Reading the Kent Beck's Extreme Programming book I stumbled upon the idea of Collective Ownership which is "Anyone can change any code anywhere in the system at any time." I see that Collective Ownership contradicts the OCP. But maybe I am mistaken.…
0
votes
0 answers

Facing architecture/design issue in SOLID Principles

Suppose I have requirement to draw a shape. The shape will be decided by backend server. I have following code: enum ShapeType { case circle case rectangle } protocol Shape { var type: ShapeType { get } func render() } struct…
Bhushan B
  • 2,470
  • 4
  • 26
  • 38
0
votes
3 answers

C++ How can I combine and add functionality to the same inherited method by a class with multiple inheritance?

So say I have the class empire. empire inherits populationContainer and landContainer as such: class empire : public populationContainer, public landContainer Both of those parent classes have the method update(), each of which do their own thing;…
0
votes
2 answers

How to avoid if..else(or any conditionals) while deciding which method to be called?

How to follow Open Close Principle without violating LSP while deciding which method to be invoked with different parameters in a statically typed language? Consider the requirement like Action 1: perform DB operation on Table 1 Action 2: Perform…
0
votes
1 answer

Is a good practice to create a new React element based on props?

This following code actualy works, but I wonder if it's a good practice or not. Because to me, it sounds like I breaking the rules. But I dont see other ways to do what I want. And I want to render 2 lists build with the same logic but that render…
foxwit
  • 315
  • 2
  • 6
0
votes
1 answer

Does this violates open/closed principle?

I made a webapi with .NET Core and I have a "Startup" class. This class has a "ConfigureServices" method that says: // This method gets called by the runtime. Use this method to add services to the container. But every time i add a new service, i…
Andrews
  • 33
  • 6
0
votes
0 answers

how to not break Open-Close principle when action is not obvious

let say we have some hardcoded actions like below: enum ActionType { case "goToScreenA" case "goToScreenB" case "presentAlert" ... } and also these actions will receive from JSON like below: struct Action { let type: String // one…
Hoven
  • 563
  • 1
  • 5
  • 24
0
votes
1 answer

Prisma generated types can hinder changing the database layer later on?

I recently heard about prisma and wanted to try it. I really liked the simple interface it provides and the level of type saftey. however, I have one question. when we use prisma to query an entity, the response we get has a type that comes from…
0
votes
2 answers

Is it a good practice to make concrete classes final?

Open-Closed principle is one of the five SOLID principles of object-oriented design. It states that "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification". But recently most programming…
Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
0
votes
0 answers

Java game classes: Hero vs Monsters

Hello I want to solve this problem: You are making a role playing game. First, you'll need a hero whose role to play. As in all RPGs the hero has health points (life), attack strength and defense. He also has experience points, and a name, given at…
0
votes
1 answer

Classes with verb names in Serenity

So I been reading Clean Code by Robert C. Martin. He suggests to use verbs for functions and nouns for Classes. However I been working with Serenity BDD framework using screen play pattern. Where the idea is to have a separate class for each…
0
votes
1 answer

Should I create classes for each different constructor or should i just create two constructor for one class?

I am new to oop.so i am just experimenting some stuffs.The below problem is just a made up problem but it would help me to understand oop more clearly. I am coding to make a calculator which can only add and multiply deciaml number and binary…
0
votes
1 answer

What Design Principle is Violated, and Which Design Pattern can Fix It

In one recent exam, I was asked 2 questions regarding the code snippet below... The questions are as follows Identify the design principle violated by the code snippet Describe the design pattern that solves the design principle violated. Provide…
0
votes
1 answer

Openshift/OCP-> not able to resolve the hostname within same namespace. Service is mapping to the POD name which is not resolvable

I am new to Kubernetes/OCP world and deploying my application to OCP namespace and trying to connect to gemfire server which is also in same namespace. To access I have created a ClusterIP Service: ocp-gemfire. This service exposes 10334 and 40404…
parinda
  • 21
  • 4
0
votes
1 answer

How can I prevent violating the open closed principle when using data from a database

I am fetching json data from a database formatted in the following way { name:{ type:string, value:'test', }, age:{ type:number, value:6, }, hairColor:{ type:color, …
anonymous-dev
  • 2,897
  • 9
  • 48
  • 112