6

I read articles about S.O.L.I.D. but I don't see any difference between OCP and DIP. Look at this example of OCP:

http://www.oodesign.com/open-close-principle.html

The code which holds OCP also fulfils the DIP. Can anyone give me an example of code that holds OCP and not DIP?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
santino
  • 159
  • 2
  • 11
  • OCP and DIP are fundamentally different from a conceptual perspective. My guess is that code that holds OCP may or may not also hold DIP, but either way it's incidental. – Brian Driscoll Nov 30 '11 at 15:10
  • but at least for me DIP implies OCP, so why we need OCP? – santino Nov 30 '11 at 15:12
  • DIP may imply OCP, but OCP does not necessarily imply DIP. – Brian Driscoll Nov 30 '11 at 15:14
  • so, as I understand OCP is DIP or DIP without an abstraction. Am I correct? – santino Nov 30 '11 at 15:15
  • I think you're thinking too much in terms of code and not really thinking about the concepts. OCP and DIP are both conceptual principles about how to design an application, and as such they are different. – Brian Driscoll Nov 30 '11 at 15:20
  • OK, i see there are different motivations. I wanted to cut out the OCP, as the unnecessary axiom, but that's not the point. Thanks for help Brian. – santino Nov 30 '11 at 15:24
  • Does this answer your question? [What is difference between the Open/Closed Principle and the Dependency Inversion Principle?](https://stackoverflow.com/questions/18428027/what-is-difference-between-the-open-closed-principle-and-the-dependency-inversio) – jaco0646 Sep 23 '21 at 00:49

1 Answers1

4

I find the explanations of dependency injection and open/closed to be confusing as well. It doesn't have to be that way. Let's take a look at the article you reference: http://www.oodesign.com/open-close-principle.html

In their example, a there is a GraphicsEditor class and a hierarchy of shape classes. In the first class diagram they show, there are a bunch of methods in the GraphicsEditor for drawing each kind of shape class: drawShape; drawCircle; drawRectangle.

What happens when you want to add a Parallelogram class? You first create the new class Parallelogram and then you modify the the GraphicsEditor class to add a new method called drawParallelogram.

This is the "badness" the article refers to: adding one new shapes means you have to change existing code. You add a new subclass of Shape (Parallelogram) and you add a new method to GraphicsEditor (drawParallelogram).

That might not seem like a big deal, but it does not scale. Imagine a team of 20 developers all working on the software at once. First, every developer that adds a new shape has to remember to do two things: updated exiting code and create new code. Every new developer that joins the project will probably learn this the hard way. Second, if everyone is adding new shapes every day, it means everyone is trying to edit the GraphicsEditor class. At the same time. It's a headache. Ask me how I know that. :-) (There is also a chance of introducing a bug into existing code when it is modified.)

It would be ideal if you could add a new Shape to the system without touching ANY code in the GraphicsEditor class. That is what the article wants to demonstrate.

Look at the second class diagram in the article. Each shape now implements its own draw method. The GraphicsEditor only needs to know that there is some superclass, "Shape", and that all of its subclasses implement the method "draw". The GraphicsEditor no longer cares how many subclasses there are or what their names are. Developers are free to implement new shapes without having to modify the GraphicsEditor class. The GraphicsEditor class is now "closed". In this way, the system is "open to extension"-- no existing code has to be changed to create new shapes. Problem solved.

A simpler way to understand all of this is to learn the Visitor Design Pattern. I don't like Wikipedia's explanation of this pattern, so I will point you to another place: http://sourcemaking.com/design_patterns/visitor. I think that understanding the Visitor Pattern makes all the other terms and concepts fall into place.

ahoffer
  • 6,347
  • 4
  • 39
  • 68