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
4
votes
3 answers

How to not violate Open-Close Principle when new feature is requested?

I need to add a new functionality in a project and I am trying to do this in the best way. So new feature = > Open close principle. I am supposed not to change existing code, am I right ? Here is the interface: public interface…
4
votes
1 answer

Java - Visitor pattern for adding methods

So i created a simple visitor pattern for liquids. Such as Milk,juice and liquor. a Milk class can look like this: public class Milk implements Visitable{ public float tax=0; @Override public int price() { return 4; } @Override public void…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
4
votes
3 answers

difference between open closed principle and inheritance

I know that open closed principle mean open for extension and closed for modification. Consider an example as follows public class Vehicle{ public void service(){ //vehicle servicing code } } public class Bike extends Vehicle{ …
4
votes
7 answers

Why does encapsulation seem to contradict the Open-Closed principle?

In encapsulation the idea is that you hide what your class is doing with its variables by declaring them private, which achieves the OCP. But then why would you then add getters and setters, which then opens up your variables for modification? Why…
liloka
  • 1,016
  • 4
  • 14
  • 29
4
votes
4 answers

Design Pattern - Abstract Factory pattern and Open Closed Principle

I am a beginner in design patterns. I am trying to use Abstract Factory - pattern while maintaining Open-Closed Principle. Plz look at the following code: public interface IAbstractFormFactory { void ShowOSName(); } public…
user366312
  • 16,949
  • 65
  • 235
  • 452
3
votes
2 answers

Advantages of Acyclic Visitor over Command with Switch On Type

The visitor pattern is useful in situations where the element hierarchy is stable and the desired functionality for operating on those elements changes often. In cases where the element hierarchy changes, the visitor pattern suffers from coupling…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
3
votes
2 answers

Open/Closed principle query

Just a quick one, so following the open closed principle, if you had a class like so: public class Employee extends Person { int age; String name; Employee(int age, String name) { super(age, name) } // Getters and…
MaxCulley
  • 59
  • 1
  • 8
3
votes
3 answers

Confusion about open/closed principal

Open/closed principle states that classes are closed for modifications but open for extensions. Lets say we want to design a payment system where payment can be processed by multiple processors like following: class Payment { void…
3
votes
1 answer

Is extension methods follow open close principle

My question is about extension method and open close principle: Do creating extensions methods for classes follow open-close principle? How do I test classes that use extension methods?
Dave
  • 43
  • 2
3
votes
2 answers

How to avoid type checking in C# to load correct UI?

I am building a simple quiz program with WinForms that has 2 modes: 1) Edit mode: Where the user can create its own questions 2) Quiz mode: Where the user needs to answer the questions Currently there are 2 type of questions: Open (question and free…
SagiZiv
  • 932
  • 1
  • 16
  • 38
3
votes
3 answers

How to instantiate a class as the interface that it derives from with constrained generic type parameter

There's the following interface which defines a packet. public interface IPacket { int Size { get; } } There are two implementations, each with its own additional property. public class FooPacket : IPacket { public int Size => 10; …
Moss
  • 855
  • 1
  • 9
  • 23
3
votes
3 answers

Typescript - Open-closed principle

I want to use OCP rule in my "small" project. Actually i have a problem with implementing this one. Let me show you what have i wrote. abstract class AnimationType { public abstract createAnimation(timing: number): { in: string, out: string…
user6372887
3
votes
1 answer

Advantage of factory method pattern

From wiki, The Factory Method design pattern solves problems like: How can an object be created so that subclasses can redefine which class to instantiate? How can a class defer instantiation to subclasses? For example, MazeGame provides the…
overexchange
  • 15,768
  • 30
  • 152
  • 347
3
votes
2 answers

Making sense of the open closed principle

I am trying to get my head around the open closed principle: Modules should be open for extension and closed for modification In terms of java is the extension specifically inheritance or does it include inheritance as well as the addition of new…
berimbolo
  • 3,319
  • 8
  • 43
  • 78
3
votes
3 answers

Do we need to adhere to the Open-Closed Principle if we have good test coverage?

Why Open-Closed Principle is such a big deal? If you have tests why you should worry about code modifications?
Romper
  • 2,009
  • 3
  • 24
  • 43