The Open/Closed Principle states that software entities (classes, modules, etc.) should be open for extension, but closed for modification. What does this mean, and why is it an important principle of good object-oriented design?
...
I have a class to which I'm constantly adding to.
public class OrderRepository{
public void Add(IEnumerable<Order> orders){}
public void Update(IEnumerable<Order> orders){}
public void Remove(IEnumerable<Order> orders){}
public void Expedite(IEnumerable<Order> orders){}
public void GetOrderData(Order order, DateTime...
The open-closed principle states that "Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification".
However, Joshua Bloch in his famous book "Effective Java" gives the following advice: "Design and document for inheritance, or else prohibit it", and encourages programmers to use the "...
How do you think about data access code like this:
public void AddCusotmer(Cusotmer customer)
{
//save customer into database
...
// save payment type
SavePaymentType(customer);
//save other data
...
}
private void SavePaymentType(Customer customer)
{
if(customer.PaymentType is XXXPayment)
{
var payment ...
OCP (Open/Closed Principle) is one of the SOLID principles. Which is says:
”Software Entities should be Open for Extension, but Closed for Modification.”
It take me while to understand the above sentence about OCP. And when I start read more about it, I found it make sense and so useful, but in the mean time I noticed it cause duplica...
This question is not about what OCP is. And I am not looking for simplistic answers, either.
So, here is why I ask this. OCP was first described in the late 80s. It reflects the thinking and context of that time. The concern was that changing source code to add or modify functionality, after the code had already been tested and put into...
The Wikipedia article on OCP says (emphasis mine):
... the open/closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"... This is especially valuable in a production environment, where changes to source code may necessitate code reviews, unit tests, and ...
I'm designing an API for the first time, and trying to follow SOLID guidelines. One of the things I find myself struggling with is balancing OCP and testability with simplicity and ease of extensibility.
This open-source API is geared toward scientific modeling and computation. The aim is that various groups will be able to easily impor...
Hi,
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 class SpecificObject2 : SomeObject
{}
// Smelly code
public class Model
{
publ...
I have recently been trying to learn about basic design principles and the OCP has me a bit confused. It makes sense that when a change happens it is preferable to extend the system rather than modify existing and working parts. But isn't this more of a principle for how to implement changes in a system rather than how to design one? Isn...