What is the meaning and reasoning behind the Open/Closed Principle?
Asked Answered
Q

14

64

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?

Quiroz answered 12/9, 2008 at 13:48 Comment(0)
D
34

Specifically, it is about a "Holy Grail" of design in OOP of making an entity extensible enough (through its individual design or through its participation in the architecture) to support future unforseen changes without rewriting its code (and sometimes even without re-compiling **).

Some ways to do this include Polymorphism/Inheritance, Composition, Inversion of Control (a.k.a. DIP), Aspect-Oriented Programming, Patterns such as Strategy, Visitor, Template Method, and many other principles, patterns, and techniques of OOAD.

** See the 6 "package principles", REP, CCP, CRP, ADP, SDP, SAP

Daggett answered 12/9, 2008 at 16:21 Comment(2)
Do all those "ways to do this" involve inheritance at some point? (I don't like creating interfaces just in case we might want more than one way of doing something in the future).Imminence
@PaulSumpner some non-inheritance examples: Composition, Inversion of Control (a.k.a. DIP), Aspect-Oriented Programming, Patterns such as Strategy, VisitorDaggett
S
73

It means that you should put new code in new classes/modules. Existing code should be modified only for bug fixing. New classes can reuse existing code via inheritance.

Open/closed principle is intended to mitigate risk when introducing new functionality. Since you don't modify existing code you can be assured that it wouldn't be broken. It reduces maintenance cost and increases product stability.

Sutherlan answered 12/9, 2008 at 14:1 Comment(1)
Indeed, this is what OCP is about. I believe the goal of "reduce maintenance cost and increase product stability" can be achieved in other way, though: by embracing change through KISS, YAGNI, agile development, TDD, and refactoring, always with a good automated test suite as a safety net.Exuviate
D
34

Specifically, it is about a "Holy Grail" of design in OOP of making an entity extensible enough (through its individual design or through its participation in the architecture) to support future unforseen changes without rewriting its code (and sometimes even without re-compiling **).

Some ways to do this include Polymorphism/Inheritance, Composition, Inversion of Control (a.k.a. DIP), Aspect-Oriented Programming, Patterns such as Strategy, Visitor, Template Method, and many other principles, patterns, and techniques of OOAD.

** See the 6 "package principles", REP, CCP, CRP, ADP, SDP, SAP

Daggett answered 12/9, 2008 at 16:21 Comment(2)
Do all those "ways to do this" involve inheritance at some point? (I don't like creating interfaces just in case we might want more than one way of doing something in the future).Imminence
@PaulSumpner some non-inheritance examples: Composition, Inversion of Control (a.k.a. DIP), Aspect-Oriented Programming, Patterns such as Strategy, VisitorDaggett
U
12

More specifically than DaveK, it usually means that if you want to add additional functionality, or change the functionality of a class, create a subclass instead of changing the original. This way, anyone using the parent class does not have to worry about it changing later on. Basically, it's all about backwards compatibility.

Another really important principle of object-oriented design is loose coupling through a method interface. If the change you want to make does not affect the existing interface, it really is pretty safe to change. For example, to make an algorithm more efficient. Object-oriented principles need to be tempered by common sense too :)

Uncle answered 12/9, 2008 at 14:1 Comment(0)
R
10

Let's break down the question in three parts to make it easier to understand the various concepts.


Reasoning Behind Open-Closed Principle

Consider an example in the code below. Different vehicles are serviced in a different manner. So, we have different classes for Bike and Car because the strategy to service a Bike is different from the strategy to service a Car. The Garage class accepts various kinds of vehicles for servicing.

Problem of Rigidity

Observe the code and see how the Garage class shows the signs of rigidity when it comes to introducing a new functionality:

class Bike {
    public void service() {
        System.out.println("Bike servicing strategy performed.");
    }
}

class Car {
    public void service() {
        System.out.println("Car servicing strategy performed.");
    }
}

class Garage {
    public void serviceBike(Bike bike) {
        bike.service();
    }

    public void serviceCar(Car car) {
        car.service();
    }
}

As you may have noticed, whenever some new vehicle like Truck or Bus is to be serviced, the Garage will need to be modified to define some new methods like serviceTruck() and serviceBus(). That means the Garage class must know every possible vehicle like Bike, Car, Bus, Truck and so on. So, it violates the open-closed principle by being open for modification. Also it's not open for extension because to extend the new functionality, we need to modify the class.


Meaning Behind Open-Closed Principle

Abstraction

To solve the problem of rigidity in the code above we can use the open-closed principle. That means we need to make the Garage class dumb by taking away the implementation details of servicing of every vehicle that it knows. In other words we should abstract the implementation details of the servicing strategy for each concrete type like Bike and Car.

To abstract the implementation details of the servicing strategies for various types of vehicles we use an interface called Vehicle and have an abstract method service() in it.

Polymorphism

At the same time, we also want the Garage class to accept many forms of the vehicle, like Bus, Truck and so on, not just Bike and Car. To do that, the open-closed principle uses polymorphism (many forms).

For the Garage class to accept many forms of the Vehicle, we change the signature of its method to service(Vehicle vehicle) { } to accept the interface Vehicle instead of the actual implementation like Bike, Car etc. We also remove the multiple methods from the class as just one method will accept many forms.

interface Vehicle {
    void service();
}

class Bike implements Vehicle {
    @Override
    public void service() {
        System.out.println("Bike servicing strategy performed.");
    }
}

class Car implements Vehicle {
    @Override
    public void service() {
        System.out.println("Car servicing strategy performed.");
    }
}

class Garage {
    public void service(Vehicle vehicle) {
        vehicle.service();
    }
}

Importance of Open-Closed Principle

Closed for modification

As you can see in the code above, now the Garage class has become closed for modification because now it doesn't know about the implementation details of servicing strategies for various types of vehicles and can accept any type of new Vehicle. We just have to extend the new vehicle from the Vehicle interface and send it to the Garage. That's it! We don't need to change any code in the Garage class.

Another entity that's closed for modification is our Vehicle interface. We don't have to change the interface to extend the functionality of our software.

Open for extension

The Garage class now becomes open for extension in the context that it will support the new types of Vehicle, without the need for modifying.

Our Vehicle interface is open for extension because to introduce any new vehicle, we can extend from the Vehicle interface and provide a new implementation with a strategy for servicing that particular vehicle.

Strategy Design Pattern

Did you notice that I used the word strategy multiple times? That's because this is also an example of the Strategy Design Pattern. We can implement different strategies for servicing different types of Vehicles by extending it. For example, servicing a Truck has a different strategy from the strategy of servicing a Bus. So we implement these strategies inside the different derived classes.

The strategy pattern allows our software to be flexible as the requirements change over time. Whenever the client changes their strategy, just derive a new class for it and provide it to the existing component, no need to change other stuff! The open-closed principle plays an important role in implementing this pattern.


That's it! Hope that helps.

Ravenous answered 26/10, 2020 at 13:1 Comment(7)
explained very well.Endocardial
The BEST explanationMenado
It just explanation of a "strategy" pattern which has a side effect to show how can be implemented open-closed principle. Or it's the only way to follow it - with any class apply the strategy pattern?Gono
@Gono The two concepts are intervowen. Open-closed principle is mostly used for being able to extend the functionality of the existing code. But it can also be used when you want to be able to easily change the strategies.Ravenous
@YogeshUmeshVaity I mean what we should do to satisfy an OCP in case in future our Garage will need additionally get an errors list of the Vehicle and report it to some new component let's name it ReportingService? Thus I imagine an one new method "getErrors" on Vehicle interface will be added and new functionality should be added into Garage#service(...) which will invoke Vehicle#getErrors and then send the result to ReportingService. So the above modifications are exactly thing which is forbidden by OCP so how we should solve it?Gono
@Gono In that case, you implement the Vehicle interface, say class VehicleWithErrors implements Vehicle. This class will have a reference to your ReportingService object and a list of errors returned by the new getErrors() method. In the service() method's implementation, you report the errors to the ReportingService. The Garage class remains unmodified.Ravenous
@YogeshUmeshVaity I think this idea(among with main idea) can be improved by instead of put 'service' method into Vehicle, introduce some another interface let's name it 'Service' which has method "handle" with one argument of type "Vehicle" and it's implementations relatively will handle servicing a vehicle and handle errors when relevant implementation is given. Anyway I've got the main principal, thank you!Gono
W
9

Open Closed Principle is very important in object oriented programming and it's one of the SOLID principles.

As per this, a class should be open for extension and closed for modification. Let us understand why.

class Rectangle {
    public int width;
    public int lenth;
}

class Circle {
    public int radius;
}

class AreaService {
    public int areaForRectangle(Rectangle rectangle) {
        return rectangle.width * rectangle.lenth;
    }

    public int areaForCircle(Circle circle) {
        return (22 / 7) * circle.radius * circle.radius;
    }
}

If you look at the above design, we can clearly observe that it's not following Open/Closed Principle. Whenever there is a new shape(Tiangle, Square etc.), AreaService has to be modified.

With Open/Closed Principle:

interface Shape{
    int area();
}

class Rectangle implements Shape{
    public int width;
    public int lenth;

    @Override
    public int area() {
        return lenth * width;
    }
}

class Cirle implements Shape{
    public int radius;

    @Override
    public int area() {
        return (22/7) * radius * radius;
    }
}

class AreaService {
    int area(Shape shape) {
        return shape.area();
    }
}

Whenever there is new shape like Triangle, Square etc. you can easily accommodate the new shapes without modifying existing classes. With this design, we can ensure that existing code doesn't impact.

Wield answered 17/4, 2020 at 11:24 Comment(4)
This change breaks Single Responsibility Principle with shapes calculating their own areas.Clarance
@User10482 It doesn'tMaddis
@GiovanniSilva I have come to realize that these SOLID principles are highly subjective. Is calculating the area the responsibility of the shapes? Maybe, so sure.Clarance
Each shape has its own formula to calculate its area, and it owns the data necessary for that (no argument against this I think), if you externalize the calculation, then you have to take into consideration which shape is being calculated, and this is a violation of the SOLID principle. If the responsibility moves to shape, then you can consume the calculation in an abstract way no matter the shape, and the code evolves freely. SOLID is about abstraction and code maintenance; when we think about that, it becomes less subjective, but it still depends on the design and object of the program.Maddis
E
8

Software entities should be open for extension but closed for modification

That means any class or module should be written in a way that it can be used as is, can be extended, but neve modified

Bad Example in Javascript

var juiceTypes = ['Mango','Apple','Lemon'];
function juiceMaker(type){
    if(juiceTypes.indexOf(type)!=-1)
        console.log('Here is your juice, Have a nice day');
    else
        console.log('sorry, Error happned');
}

exports.makeJuice = juiceMaker;

Now if you want to add Another Juice type, you have to edit the module itself, By this way, we are breaking OCP .

Good Example in Javascript

var juiceTypes = [];
function juiceMaker(type){
    if(juiceTypes.indexOf(type)!=-1)
        console.log('Here is your juice, Have a nice day');
    else
        console.log('sorry, Error happned');
}
function addType(typeName){
    if(juiceTypes.indexOf(typeName)==-1)
        juiceTypes.push(typeName);
}
function removeType(typeName){
  let index = juiceTypes.indexOf(typeName)
    if(index!==-1)
        juiceTypes.splice(index,1);
}

exports.makeJuice = juiceMaker;
exports.addType = addType;
exports.removeType = removeType;

Now, you can add new juice types from outside the module without editing the same module.

Extravagate answered 4/7, 2017 at 20:33 Comment(1)
In practical, it's not possible to achive can be extended, but neve modified.Bandwidth
M
6

It's the answer to the fragile base class problem, which says that seemingly innocent modifications to base classes may have unintended consequences to inheritors that depended on the previous behavior. So you have to be careful to encapsulate what you don't want relied upon so that the derived classes will obey the contracts defined by the base class. And once inheritors exist, you have to be really careful with what you change in the base class.

Mcquiston answered 12/9, 2008 at 13:53 Comment(1)
I thought this was the LSP (Liskov Substitution Principle) instead?Exuviate
C
4

Purpose of the Open closed Principle in SOLID Principles is to

  1. reduce the cost of a business change requirement.
  2. reduce testing of existing code.

Open Closed Principle states that we should try not to alter existing code while adding new functionalities. It basically means that existing code should be open for extension and closed for modification(unless there is a bug in existing code). Altering existing code while adding new functionalities requires existing features to be tested again.

Let me explain this by taking AppLogger util class.

Let's say we have a requirement to log application wide errors to a online tool called Firebase. So we create below class and use it in 1000s of places to log API errors, out of memory errors etc.

open class AppLogger {

    open fun logError(message: String) {
        // reporting error to Firebase
        FirebaseAnalytics.logException(message)
    }
}

Let's say after sometime, we add Payment Feature to the app and there is a new requirement which states that only for Payment related errors we have to use a new reporting tool called Instabug and also continue reporting errors to Firebase just like before for all features including Payment.

Now we can achieve this by putting an if else condition inside our existing method

fun logError(message: String, origin: String) {
    if (origin == "Payment") {
        //report to both Firebase and Instabug
        FirebaseAnalytics.logException(message)
        InstaBug.logException(message)
    } else {
        // otherwise report only to Firebase
        FirebaseAnalytics.logException(message)
    }
}

Problem with this approach is that it violates Single Responsibility Principle which states that a method should do only one thing. Another way of putting it is a method should have only one reason to change. With this approach there are two reasons for this method to change (if & else blocks).

A better approach would be to create a new Logger class by inheriting the existing Logger class like below.

class InstaBugLogger : AppLogger() {

    override fun logError(message: String) {
        super.logError(message) // This uses AppLogger.logError to report to Firebase.
        InstaBug.logException(message) //Reporting to Instabug
    }
}

Now all we have to do is use InstaBugLogger.logError() in Payment features to log errors to both Instabug and Firebase. This way we reduce/isolate the testing of new error reporting requirement to only Payment feature as code changes are done only in Payment Feature. The rest of the application features need not be tested as there are no code changes done to the existing Logger.

Carangid answered 28/7, 2021 at 20:5 Comment(0)
H
2

The principle means that it should easy to add new functionality without having to change existing, stable, and tested functionality, saving both time and money.

Often, polymorhism, for instance using interfaces, is a good tool for achieving this.

Humidify answered 12/9, 2008 at 13:54 Comment(0)
T
2

An additional rule of thumb for conforming to OCP is to make base classes abstract with respect to functionality provided by derived classes. Or as Scott Meyers says 'Make Non-leaf classes abstract'.

This means having unimplemented methods in the base class and only implement these methods in classes which themselves have no sub classes. Then the client of the base class cannot rely on a particular implementation in the base class since there is none.

Trahan answered 16/11, 2008 at 21:17 Comment(0)
B
1

I just want to emphasize that "Open/Closed", even though being obviously useful in OO programming, is a healthy method to use in all aspects of development. For instance, in my own experience it's a great painkiller to use "Open/Closed" as much as possible when working with plain C.

/Robert

Brazil answered 8/12, 2008 at 13:59 Comment(0)
T
0

This means that the OO software should be built upon, but not changed intrinsically. This is good because it ensures reliable, predictable performance from the base classes.

Trangtranquada answered 12/9, 2008 at 13:50 Comment(0)
F
0

I was recently given an additional idea of what this principle entails: that the Open-Closed Principle describes at once a way of writing code, as well as an end-result of writing code in a resilient way.

I like to think of Open/Closed split up in two closely-related parts:

  • Code that is Open to change can either change its behavior to correctly handle its inputs, or requires minimum modification to provide for new usage scenarios.
  • Code that is Closed for modification does not require much if any human intervention to handle new usage scenarios. The need simply does not exist.

Thus, code that exhibits Open/Closed behavior (or, if you prefer, fulfills the Open/Closed Principle) requires minimal or no modification in response to usage scenarios beyond what it was originally built for.

As far as implementation is concerned? I find that the commonly-stated interpretation, "Open/Closed refers to code being polymorphic!" to be at best an incomplete statement. Polymorphism in code is one tool to achieve this sort of behavior; Inheritance, Implementation...really, every object-oriented design principle is necessary to write code that is resilient in the way implied by this principle.

Ferrous answered 18/5, 2012 at 20:23 Comment(0)
C
0

In Design principle, SOLID – the "O" in "SOLID" stands for the open/closed principle.

Open Closed principle is a design principle which says that a class, modules and functions should be open for extension but closed for modification.

This principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code (tested code). The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged.

Benefit of Open Closed Design Principle:

  1. Application will be more robust because we are not changing already tested class.
  2. Flexible because we can easily accommodate new requirements.
  3. Easy to test and less error prone.

My blog post on this:

http://javaexplorer03.blogspot.in/2016/12/open-closed-design-principle.html

Cody answered 23/12, 2016 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.