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

How Class can change another class implementation without inherit it

I am trying to pass OCP 8 and I face this code which can override a class method on the fly as it treats it as abstract or interface package xxx.xxx.ClassC; public class ClassC{ public void print(){`enter code here` …
Zero Code
  • 19
  • 1
  • 3
0
votes
1 answer

How to implement factory pattern that dynamically loads new products (open close principle)?

In factory method, we have to write switch case or if statements to decide which instance to create and return. This violates the open close principle because - everytime a new product is to be added, the factory method code has to be updated. Is…
variable
  • 8,262
  • 9
  • 95
  • 215
0
votes
2 answers

Is this the Open/Closed principle? And if not

Considering the following code public interface IEntity { int Id { get; set; } } public class User : IEntity { public int Id { get; set; } } public abstract class RepositoryBase where TEntity : IEntity { public bool Save(TEntity…
Dave Archer
  • 3,022
  • 20
  • 23
0
votes
1 answer

Implementation of Open-Closed Principle with frequent changes

I have a requirement where system sends email. Currently the system sends user's first name in email and it's in production and working fine. Now my client asked to add last name as well in email, so I extended the send(User userinfo) method and…
0
votes
2 answers

Making a class follow OCP - Factoring functions into objects

I have a class to which I'm constantly adding to. public class OrderRepository{ public void Add(IEnumerable orders){} public void Update(IEnumerable orders){} public void Remove(IEnumerable orders){} public void…
0
votes
1 answer

Applying SRP and OCP

I have been trying to apply SRP and OCP to this code. The code should allow the Bank Employee to book or not book the hard coded appointments. I can't figure out what classes to create to apply these two principles to the code. I want to know what…
0
votes
2 answers

Java and Comparable

I'm new here and this is my first post. I've just completed my Java OCA and now moving onto studying for the OCP. I have a question regarding Comparable interface. I have this code snippet which explains how Comparable is implemented: import…
Bobby Iveson
  • 133
  • 8
0
votes
1 answer

How to use the Open/Closed Principle to replace switch block that modifies shared state

I am working on a project that requires me to generate a report based on purchased rentals. I have to keep a count for each type of rental and sum the corresponding totals. Currently I am using a switch block to determine which action to take based…
yCombinator
  • 113
  • 4
  • 15
0
votes
0 answers

Open/Closed principal multiple levels

The following class breaks the open/closed principal: public class Account { public decimal Interest { get; set; } public decimal Balance { get; set; } public decimal CalcInterest(string accType) { if (accType ==…
Sun
  • 4,458
  • 14
  • 66
  • 108
0
votes
1 answer

Is the Open-Closed SOLID principle the same as Coding to an Interface?

Does open-for-extension and closed-for-modification mean code-to-an-interface? If I code to an interface so that future concrete implementations can be introduced by implementing the interface, and we create new classes without touching existing…
0
votes
1 answer

How to calculate area using Open closed principle C#

I am working with Open Closed principle of SOLID in C#. I have abstract class Shape which i want to used to calculate the area of different shapes. How to call areaCalculator class and how to pass different shapes. Here is my code. public abstract…
Kamran Khan
  • 1,042
  • 10
  • 21
0
votes
3 answers

Open/Closed for flexible software

The title may not be too descriptive, but I couldn't think of a better one. I'm sorry for that. So, the problem I am having here is one I have come across a couple of times now. It's really about design patterns and principles, and is language…
Jasper
  • 11,590
  • 6
  • 38
  • 55
0
votes
3 answers

How to convert from conditional statements to OCP (Open Closed Principle) in SOLID?

I have a code that performs various database checks based on the passed string value. I can solve it with multiple conditional statements(if and else) and I definitely know it's breaking the OCP in Solid principle because it is open to modification.…
0
votes
1 answer

OOP and S.O.L.I.D based alternative to hard coding

I've got to refactor this code as seeing from SOLID viewpoint it is not a good practice(not extensible), what are the alternatives? I have a text file from which i read the commands, its format goes like: ADD_CHILD name1 name2…
0
votes
1 answer

C# Interfaces & Generics working together

I have a domain object, Address that may be populated from a variety of data sources, which requires a lot of mapping code. In the interest of "Closed to Modification" I want to be able to create individual "Mappers" for each data source. I can then…