Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open / Closed principle - How to call the new versions?

I'm trying to grasp the Open/Closed principle (in my case for PHP, but that shouln't really make a difference).

The way i understand it is that a class is never open for modification. Only for bug fixing. If i wanted to add new code to the class, then i'd have to create a new one and extend the 'old' class. That's the only way i can add new code to it.

In a way i can see the advantages of this. Because basically you create some sort of versioning system, where old code always work, but you can always try to use the new classes too.

But how does this work in practice? I mean, suppose i have the following class:

class MyObject
{
    public function doSomething()
    {
        echo 'Im doing something';
    }
}

So somewhere i'm probably instantiating this class:

$obj = new MyObject();

But then i decide that it's good to have another method in that object. So i can do something else too. According to the OCP i can't modify the class. So i have to create a new one, which extends to old one right?

First problem. How do i call the new class? Because it isn't really a complete new object. Like. a User object is a User object. I can't suddenly give it completely diffent name just because it needs another method. Anyway, i create the new class:

class MyNewObject extends MyObject
{
    public function doSomethingElse()
    {
        echo 'Im doing something else now';
    }
}

Now this also means i have to change the line of code where i instantiated the "MyObject" class and replace it with the "MyNewObject" class, right..? And if that's done in more than one place, then i have to search through my source code... (Think about a method in a controller class, which almost always uses the 'new' keyword to instantiate certain classes).

The same basically applies to inheritance. I'd have to find each class the inherits the old class and have to replace that with the new class.


So basically my questions are:

How do you name the new classes which have the new methods? Just becasue i added some new functionality, doesn't mean i can give the class a whole new name...

And what if the 'old' classs is instantiated (or inherited) from multiple places. Then i'd have to find all of those places... Where's the gain?

like image 572
w00 Avatar asked May 23 '12 20:05

w00


2 Answers

The Open Closed Principle isn't intended to be used as a kind of version control system. If you really need to make changes to the class, then go ahead and make those changes. You don't need to create new classes and change all the places that instantiated the old one.

The point of the Open Closed Principle is that a well-designed system shouldn't require you to change existing functionality in order to add new functionality. If you are adding a new class to the system, you shouldn't need to search through all your code to find the places where you need to reference that class or have special cases for it.

If the design of your class isn't flexible enough to handle some new piece of functionality, then by all means change the code in your class. But when you change the code, make it flexible so you can handle similar changes in the future without code changes. It's meant to be a design policy not a set of handcuffs to prevent you from making changes. With good design decisions, over time your existing code will require fewer and fewer changes when you add new functionality to the system. It's an iterative process.

like image 51
David Buck Avatar answered Nov 11 '22 18:11

David Buck


I would argue that by adding a function, you're not modifying the class behavior.

In all the instances where doSomething() is currently being called in your app, simply by adding doSomethingElse() to the class will have no effect. Since you're not changing doSomething(), the behavior is the same as it was before.

Once you determine that your doSomething() implementation isn't cutting it for certain circumstances, you can extend the class and override doSometing(). Again, the original still behaves the same as it always did, but now you have a new doSomething() to work with also.

I realize that this goes against the strict definition of open/closed, but this is the real world, and that's how I interpreted that principle in my code.

like image 31
MStodd Avatar answered Nov 11 '22 19:11

MStodd