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

Python Open Closed Principle when your objects could be a list of instances?

I have a number of different objects that I can 'jsonify', these are custom object types: class Jsonable(ABC): @abstractmethod def extract_json(): pass # return json of self class Organization(Jsonable): # implements the…
symon
  • 670
  • 1
  • 7
  • 20
0
votes
1 answer

Doing an Enthuware OCP question. Can't understand this at all

What will the following code fragment print? Path p1 = Paths.get("c:\\personal\\.\\photos\\..\\readme.txt"); Path p2 = Paths.get("c:\\personal\\index.html"); Path p3 = p1.relativize(p2); System.out.println(p3); Apparently…
user11244708
0
votes
2 answers

Can I create an instance of a derived class based on a certain property defined in my abstract base class?

Basically I have an abstract class with we'll say two properties, one being abstract and some derived classes. public abstract class BaseClass { public string ID { get; set; } public abstract string Name { get; }; } public class Class1 :…
0
votes
0 answers

ASP.NET Core Startup class is not following Open/Closed Principle

Whenever there is a new dependency identified, we need to declare it into ConfigureServices method of Startup class. By the time I have added more than 30 such services and it keep on increasing by time. This is where we see volition of Open/Closed…
Vipul
  • 1,563
  • 4
  • 22
  • 46
0
votes
2 answers

Open-closed principle for list result

I'm practising how to create an open-closed principle with C# to return a list of info. but the problem is I'm getting an error in my main service class. These are my sample codes: I have this interface: public interface ISelectInfo{ bool…
jsonGPPD
  • 987
  • 4
  • 16
  • 31
0
votes
1 answer

Which two are true about the Inventory directory in Oracle 11G?

A. It is shared by all Oracle software installations on a single server. B. It is required only for Oracle Grid Infrastructure and Oracle Database software Installations. C. It can be created only if the ORACLE_BASE environment variable is set.…
bootsoon
  • 797
  • 7
  • 13
0
votes
0 answers

SOLID Open/Closed - Interface Segregation Principles

I have applied the Open/Closed principle on the code I am currently working on. The example is given here: def error_response(object) return { errs: object.to_s, status_code: :internal_server_error, has_error: true } if…
0
votes
1 answer

parallel opencl kernel execution

I am trying to execute multiple kernels in parallel. Each kernel is independent of each other. I am trying to execute the kernel on GPU. What I think is out of order execution and enqueue each kernel separately. Is that the way to approach this…
0
votes
2 answers

Throughput calculation in OpenCl

I am trying to calculate the throughput of my kernel which is written in my openCL. But I am not sure how to do that, I have tried to find some file generated after compilation which shows throughput as 0.435(" found in the .attrb file") but not…
0
votes
1 answer

How to eliminate switch-case in code?

Let's say I have a switch case that looks something like this: (just an example) switch(Type) { case MSSQL: / Connector = new MSSQLCOnnector( args); break; case MYSQL: Connector = new MYSQLConnector( …
jhthewow
  • 133
  • 1
  • 3
  • 12
0
votes
1 answer

Duplication code VS Open-Close Principle

I am really confused about two topics: 1)- Code duplication (I understand very well) 2)- Open-Close Principle (Need explanation when to use it? Which scenarios) What happened is during an implementation of a new feature, my colleague told me to add…
0
votes
1 answer

Solid OCP violation consequences

Clearing things on the beggining: I understand how to apply OCP to code below (virtual class, DI and so on, there are tons of resources on web about that) I know (but do not understand) arguments why I need to apply OCP (3 that mainly occur on web…
0
votes
1 answer

Singleton closed for modification open to extend

I would like to know how to achieve this: Assume I have a singleton class as class Global{ static let shared = Global() private init(){} } I want this class as closed to modification. But open to extend. I want to achieve result…
Farhan Arshad
  • 375
  • 3
  • 9
0
votes
1 answer

How can I design classes that follow SOLID without off loading violations of SOLID somewhere else?

I have a controller that is violating Open and Closed Principle. I'm trying to figure out how to solve this with certain conditions. Here is the implementation. struct BasicRecording { void show() {} void hide() {} }; struct AdvanceRecording { …
user3904534
  • 309
  • 5
  • 13
0
votes
2 answers

What is the most appropriate design pattern for a notification system with multiple delivery channels?

I am working on a student information where I would like to send notifications based on certain system events. For example, when a student is marked late or absent, the application will notify a list of users(parents, school admins, etc). This…