Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am trying to understand the SOLID principles of OOP and I've come to the conclusion that LSP and OCP have some similarities (if not to say more).

the open/closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification".

LSP in simple words states that any instance of Foo can be replaced with any instance of Bar which is derived from Foo and the program will work the same very way.

I'm not a pro OOP programmer, but it seems to me that LSP is only possible if Bar, derived from Foo does not change anything in it but only extends it. That means that in particular program LSP is true only when OCP is true and OCP is true only if LSP is true. That means that they are equal.

Correct me if I'm wrong. I really want to understand these ideas. Great thanks for an answer.

share|improve this question
3  
This is a very narrow interpretation of both concepts. Open/closed can be maintained yet still violate LSP. The Rectangle/Square or Ellipse/Circle examples are good illustrations. Both adhere to OCP, yet both violate LSP. –  Joel Etherton Dec 6 '12 at 13:08
    
The world (or at least the internet) is confused on this. kirkk.com/modularity/2009/12/solid-principles-of-class-design. This guy says the violation of LSP is also violation OCP. And then in the book "Software Engineering Design: Theory and Practice" on page 156 the author gives an example of something which adheres to OCP but violates LSP. I have given up on this. –  Manoj R Dec 6 '12 at 13:28
    
@JoelEtherton Those pairs only violate LSP if they're mutable. In the immutable case, deriving Square from Rectangle does not violate LSP. (But it's probably still bad design in the immutable case since you can have square Rectangles that are not a Square which does not match mathematics) –  CodesInChaos Dec 6 '12 at 15:57
    
Simple analogy (from a library writer-user's viewpoint). LSP is like selling a product (library) that claim to implement 100% of what it says (on the interface, or user manual), but actually doesn't (or didn't match what is said). OCP is like selling a product (library) with a promise that it can be upgraded (extended) when new functionality comes out (like firmware), but actually can't be upgraded without a factory service. –  rwong Sep 10 '13 at 3:33

6 Answers 6

up vote 54 down vote accepted

Gosh, there are some weird misconceptions on what OCP and LSP and some are due to mismatch of some terminologies. Both principles are only the "same thing" if you implement them the same way. Patterns usually follow the principles in one way or another with few exceptions.

Lets take a dive into the principles and describe the discrepancy a bit better between both:

Open-Closed Principle (OCP)

According to Uncle Bob:

You should be able to extend a classes behavior, without modifying it.

Note that the word extend in this case doesn't necessarily mean that you should subclass the actual class that needs the new behavior. See how I mentioned at first mismatch of terminology? The keyword extend only means subclassing in Java, but the principles are older than Java.

The original came from Bertrand Meyer in 1988:

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.

Here it is much clearer that the principle is applied to software entities. A bad example would be override the software entity as you're modifying the code completely instead of providing some point of extension. The behavior of the software entity itself should be extensible and a good example of this is implementation of the Strategy-pattern (because it is the easiest to show of the GoF-patterns bunch IMHO):

// Context is closed for modifications. Meaning you are
// not supposed to change the code here.
public class Context {

    // Context is however open for extension through
    // this private field
    private IBehavior behavior;

    // The context calls the behavior in this public 
    // method. If you want to change this you need
    // to implement it in the IBehavior object
    public void doStuff() {
        if (this.behavior != null)
            this.behavior.doStuff();
    }

    // You can dynamically set a new behavior at will
    public void setBehavior(IBehavior behavior) {
        this.behavior = behavior;
    }
}

// The extension point looks like this and can be
// subclassed/implemented
public interface IBehavior {
    public void doStuff();
}

In the example above the Context is locked for further modifications. Most programmers would probably want to subclass the class in order to extend it but here we don't because it assumes it's behavior can be changed through anything that implements the IBehavior interface.

I.e. the context class is closed for modification but open for extension. It actually follows another basic principle because we're putting the behavior with object composition instead of inheritance:

"Favor 'object composition' over 'class inheritance'." (Gang of Four 1995:20)

I'll let the reader read up on that principle as it is outside the scope of this question. To continue with the example, say we have the following implementations of the IBehavior interface:

public class HelloWorldBehavior implements IBehavior {
    public void doStuff() {
        System.println("Hello world!");
    }
}

public class GoodByeBehavior implements IBehavior {
    public void doStuff() {
        System.out.println("Good bye cruel world!");
    }
}

Using this pattern we can modify the behavior of the context at runtime, through the setBehavior method as extension point.

// in your main method
Context c = new Context();

c.setBehavior(new HelloWorldBehavior());
c.doStuff();
// prints out "Hello world!"

c.setBehavior(new GoodByeBehavior());
c.doStuff();
// prints out "Good bye cruel world!"

So whenever you want to extend the "closed" context class, do it by subclassing it's "open" collaborating dependency. This is clearly not the same thing as subclassing the context itself yet it is OCP. LSP makes no mention about this either.

Liskov's Substitution Principle (LSP)

Uncle Bob defines it simply by:

Derived classes must be substitutable for their base classes.

This principle is old, in fact Uncle Bob's definition doesn't differentiate the principles as that makes LSP still closely related to OCP by the fact that, in the above Strategy example, the same supertype is used (IBehavior). So lets look at it's original definition by Barbara Liskov and see if we can find out something else about this principle that looks like a mathematical theorem:

What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.

Lets shrug on this for a while, notice as it doesn't mention classes at all. In JavaScript you can actually follow LSP even though it is not explicitly class-based. If your program has a list of at least a couple of JavaScript objects that:

  • needs to be computed the same way,
  • have the same behavior, and
  • are otherwise in some way completely different

...then the objects are regarded as having the same "type" and it doesn't really matter for the program. Yay for programming languages with dynamic types, right?

In generic sense; you shouldn't need to know the actual subtype if you're using it's interface. OCP does not say anything explicit about this. It also actually pinpoints a design mistake most novice programmers do:

Whenever you're feeling the urge to check the subtype of an object, you're doing it WRONG.

Okay, so it might not be wrong all the time but if you have the urge to do some type checking with instanceof or enums, you might be doing the program a bit more convoluted for yourself than it needs to be. But this is not always the case; quick and dirty hacks to get things working is an okay concession to make in my mind if the solution is small enough, and if you practice merciless refactoring, it may get improved once changes demand it.

There are ways around this "wrongness", depending on the actual problem:

  • The super class is not calling the prerequisites, forcing the caller to do so instead.
  • The super class is missing a generic method that the caller needs.

Both of these are common code design "mistakes". There are a couple of different refactorings you can do, such as pull-up method, or refactor to a pattern such the Visitor pattern.

I actually like the Visitor pattern a lot as it can take care of large if-statement spaghetti and it is simpler to implement than what you'd think on existing code. Say we have the following context:

public class Context {

    public void doStuff(string query) {

        // outcome no. 1
        if (query.Equals("Hello")) {
            System.out.println("Hello world!");
        } 

        // outcome no. 2
        else if (query.Equals("Bye")) {
            System.out.println("Good bye cruel world!");
        }

        // a change request may require another outcome...

    }

}

// usage:
Context c = new Context();

c.doStuff("Hello");
// prints "Hello world"

c.doStuff("Bye");
// prints "Bye"

The outcomes of the if-statement can be translated into their own visitors as each is depending on some decision and some code to run. We can extract these like this:

public interface IVisitor {
    public bool canDo(string query);
    public void doStuff();
}

// outcome 1
public class HelloVisitor implements IVisitor {
    public bool canDo(string query) {
        return query.Equals("Hello");
    }
    public void doStuff() {
         System.out.println("Hello World");
    }
}

// outcome 2
public class ByeVisitor implements IVisitor {
    public bool canDo(string query) {
        return query.Equals("Bye");
    }
    public void doStuff() {
        System.out.println("Good bye cruel world");
    }
}

At this point, if the programmer did not know about the Visitor pattern, he'd instead implement the Context class to check if it is of some certain type. Because the Visitor classes have a boolean canDo method, the implementor can use that method call to determine if it is the right object to do the job. The context class can use all visitors (and add new ones) like this:

public class Context {
    private ArrayList<IVisitor> visitors = new ArrayList<IVisitor>();

    public Context() {
        visitors.Add(new HelloVisitor());
        visitors.Add(new ByeVisitor());
    }

    // instead of if-statements, go through all visitors
    // and use the canDo method to determine if the 
    // visitor object is the right one to "visit"
    public void doStuff(string query) {
        for(IVisitor visitor : visitors) {
            if (visitor.canDo()) {
                visitor.doStuff();
                break; 
                // or return... it depends if you have logic 
                // after this foreach loop
            }
        }
    }

    // dynamically adds new visitors
    public void addVisitor(IVisitor visitor) {
        if (visitor != null)
            visitors.add(visitor);
    }
}

Both patterns follow OCP and LSP, however they are both pinpointing different things about them. So how does code look like if it violates one of the principles?

Violating one principle but following the other

There are ways to break one of the principles but still have the other be followed. The examples below seem contrived, for good reason, but I've actually seen these popping up in production code (and even worser):

Follows OCP but not LSP

Lets say we have the given code:

public interface IPerson {}

public class Boss implements IPerson {
    public void doBossStuff() { ... }
}

public class Peon implements IPerson {
    public void doPeonStuff() { ... }
}

public class Context {
    public Collection<IPerson> getPersons() { ... }
}

This piece of code follows the open-closed principle. If we're calling the context's GetPersons method, we'll get a bunch of persons all with their own implementations. That means that IPerson is closed for modification, but open for extension. However things take a dark turn when we have to use it:

// in some routine that needs to do stuff with 
// a collection of IPerson:
Collection<IPerson> persons = context.getPersons();
for (IPerson person : persons) {
    // now we have to check the type... :-P
    if (person instanceof Boss) {
        ((Boss) person).doBossStuff();
    }
    else if (person instanceof Peon) {
        ((Peon) person).doPeonStuff();
    }
}

You have to do type checking and type conversion! Remember how I mentioned above how type checking is a bad thing? Oh no! But fear not, as also mentioned above either do some pull-up refactoring or implement a Visitor pattern. In this case we can simply do a pull up refactoring after adding a general method:

public class Boss implements IPerson {
    // we're adding this general method
    public void doStuff() {
        // that does the call instead
        this.doBossStuff();
    }
    public void doBossStuff() { ... }
}


public interface IPerson {
    // pulled up method from Boss
    public void doStuff();
}

// do the same for Peon

The benefit now is that you don't need to know the exact type anymore, following LSP:

// in some routine that needs to do stuff with 
// a collection of IPerson:
Collection<IPerson> persons = context.getPersons();
for (IPerson person : persons) {
    // yay, no type checking!
    person.doStuff();
}

Follows LSP but not OCP

Lets look at some code that follows LSP but not OCP, it is kind of contrived but bear with me on this one it's very subtle mistake:

public class LiskovBase {
    public void doStuff() {
        System.out.println("My name is Liskov");
    }
}

public class LiskovSub extends LiskovBase {
    public void doStuff() {
        System.out.println("I'm a sub Liskov!");
    }
}

public class Context {
    private LiskovBase base;

    // the good stuff
    public void doLiskovyStuff() {
        base.doStuff();
    }

    public void setBase(LiskovBase base) { this.base = base }
}

The code does LSP because the context can use LiskovBase without knowing the actual type. You'd think this code follows OCP as well but look closely, is the class really closed? What if the doStuff method did more than just print out a line?

The answer if it follows OCP is simply: NO, it isn't because in this object design we're required to override the code completely with something else. This opens up the cut-and-paste can of worms as you have to copy code over from the base class to get things working. The doStuff method sure is open for extension, but it wasn't completely closed for modification.

We can apply the Template method pattern on this. The template method pattern is so common in frameworks that you might have been using it without knowing it (e.g. java swing components, c# forms and components, etc.). Here is that one way to close the doStuff method for modification and making sure it stays closed by marking it with java's final keyword. That keyword prevents anyone from subclassing the class further (in C# you can use sealed to do the same thing).

public class LiskovBase {
    // this is now a template method
    // the code that was duplicated
    public final void doStuff() {
        System.out.println(getStuffString());
    }

    // extension point, the code that "varies"
    // in LiskovBase and it's subclasses
    // called by the template method above
    // we expect it to be virtual and overridden
    public string getStuffString() {
        return "My name is Liskov";
    }
}

public class LiskovSub extends LiskovBase {
    // the extension overridden
    // the actual code that varied
    public string getStuffString() {
        return "I'm sub Liskov!";
    }
}

This example follows OCP and seems silly, which it is, but imagine this scaled up with more code to handle. I keep seeing code deployed in production where subclasses completely override everything and the overridden code is mostly cut-n-pasted between implementations. It works, but as with all code duplication is also a set-up for maintenance nightmares.

Conclusion

I hope this all clears out some questions regarding OCP and LSP and the differences/similarities between them. It is easy to dismiss them as the same but the examples above should show that they aren't.

Do note that, gathering from above sample code:

  • OCP is about locking the working code down but still keep it open somehow with some kind of extension points.

    This is to avoid code duplication and making breaking changes painful (i.e. change one place, break it everywhere else) by encapsulating the code that changes as with the example of Template Method pattern. For the sake of maintenance the concept of encapsulating change is a good thing, because changes always happen.

  • LSP is about letting the user handle different objects that implement a supertype without checking what the actual type they are.

    This principle provides an alternative to do type-checking and type-conversion, that can get out of hand when the number of statements grow, and can be achieved through pull-up refactoring or applying patterns such as Visitor.

share|improve this answer
4  
This is a good explanation, because it doesn't oversimplify the OCP by implying it always means implementation by inheritance. It's that oversimplification that joins OCP and SRP in some people's minds, when really they can be two completely separate concepts. –  Eric King Dec 6 '12 at 14:48
1  
This is one of the best stack exchange answers I've ever seen. I wish I could upvote it 10 times. Well done, and thank you for the excellent explanation. –  Bob Horn Dec 8 '12 at 18:55
    
There, I added a blurb on Javascript that isn't class-based programming language but can still follow LSP and edited the text so it hopefully reads more fluently. Phew! –  Spoike Dec 8 '12 at 23:43
    
While your quote from Uncle Bob from LSP is correct (same as his website), shouldn't it be the other way around? Shouldn't it state that "Base classes should be substitutable for their derived classes"? On LSP the test of "compatibility" is done against the derived class and not the base one. Still, I am not a native English speaker and I think there may be some details about the phrase that I may be missing. –  Alpha Dec 12 '12 at 19:42
    
@Alpha: That is a good question. The base class is always substitutable with it's derived classes or else inheritance wouldn't work. The compiler (at least in Java and C#) will complain if you're leaving out a member (method or attribute/field) from the extended class that needs to be implemented. LSP is meant to keep you from adding methods that are only available locally on the derived classes, as that requires the user of those derived classes to know about them. As the code grows, such methods would be difficult to maintain. –  Spoike Dec 14 '12 at 23:08

This is something that causes a lot of confusion. I prefer considering these principles somewhat philosophically, because there are many different examples for them, and sometimes concrete examples don't really capture their whole essence.

What OCP tries to fix

Say we need to add functionality to a given program. The easiest way to go about it, especially for people who were trained to think procedurally, is to add an if clause wherever needed, or something of the like.

The problems with that are

  1. It changes the flow of existing, working code.
  2. It forces a new conditional branching on every case. For example, say you have a list of books, and some of them are on sale, and you want to iterate over all of them and print their price, such that if they're on sale, the printed price will include the string "(ON SALE)".

You can do this by adding an additional field to all books named "is_on_sale", and then you can check that field when printing any book's price, or alternatively, you can instantiate on-sale books from the database using a different type, which prints "(ON SALE)" in the price string (not a perfect design but it delivers the point home).

The problem with the first, procedural solution, is an extra field for each book, and extra redundant complexity in many cases. The second solution only forces logic where it is actually required.

Now consider the fact that there could be plenty of cases where different data and logic is required, and you'll see why keeping OCP in mind while designing your classes, or reacting to changes in requirements, is a good idea.

By now you should get the main idea: Try to put yourself into a situation where new code can be implemented as polymorphic extensions, not procedural modifications.

But never be afraid to analyze the context, and see if the drawbacks happen to outweigh the benefits, because even a principle such as OCP can make a 20-class mess out of a 20-line program, if not treated carefully.

What LSP tries to fix

We all love code reuse. A disease that follows is that many programs don't understand it completely, to the point where they're blindly factoring common lines of code only to create unreadable complexities and redundant tight-coupling between modules that, other than a few lines of code, have nothing in common as far as the conceptual work-to-be-done goes.

The biggest example of this is interface reuse. You've probably witnessed it yourself; a class implements an interface, not because its a logical implementation of it (or an extension in case of concrete base classes), but because the methods it happens to declare at that point have the right signatures as far as it's concerned.

But then you encounter a problem. If classes implement interfaces only by considering the signatures of the methods they declare, then you find yourself able to pass instances of classes from one conceptual functionality into places which demand completely different functionality, that only happen to depend upon similar signatures.

That isn't that horrible, but it causes a lot of confusion, and we have the technology to prevent ourselves from making mistakes like these. What we need to do is to treat interfaces as API + Protocol. The API is apparent in declarations, and the protocol is apparent in existing uses of the interface. If we have 2 conceptual protocols which share the same API, they should be represented as 2 different interfaces. Otherwise we get caught up in DRY dogmatism and, ironically, only create harder to maintain code.

Now you should be able to understand the definition perfectly. LSP says: Don't inherit from a base class and implement functionality in those sub-classes that, other places, which depend on the base class, won't get along with.

share|improve this answer

From my understanding:

OCP says: "If you will add new functionality, create a new class extending an existing one, rather than changing it."

LSP says: "If you create a new class extending an existing class, make sure it's completely interchangable with its base."

So I think they complement each other but they are not equal.

share|improve this answer

While it's true that OCP and LSP both have to do with modification, the kind of modification OCP talks about isn't the one LSP talks about.

Modifying with regard to OCP is the physical action of a developer writing code in an existing class.

LSP deals with the behavior modification a derived class brings compared to its base class, and the runtime alteration of the program's execution that can be caused by using the subclass instead of the superclass.

So although they might look similar from a distance OCP != LSP. In fact I think they may be the only 2 SOLID principles that can't be understood in terms of each other.

share|improve this answer

LSP in simple words states that any instance of Foo can be replaced with any instance of Bar which is derived from Foo without any loss of program functionality.

This is wrong. LSP states that class Bar shouldn't introduce behavior, that is not expected when code uses Foo, when Bar is derived from Foo. It has nothing to do with loss of functionality. You can remove functionality, but only when code using Foo doesn't depend on this functionality.

But in the end, this is usually hard to achieve, because most of the time, code using Foo depends on all of it's behavior. So removing it violates LSP. But simplifying it like this is only part of LSP.

share|improve this answer
    
A very common case is where the substituted object removes side-effects: eg. a dummy logger that outputs nothing, or a mock object used in testing. –  Useless Dec 6 '12 at 13:50

I would look at it from client's point of view. if Client is using features of an interface, and internally that feature has been implemented by Class A. Suppose there is a class B which extends class A, then tomorrow if I remove class A from that interface and put class B, then class B should also provide same features to the client. Standard example is a Duck class which swims, and if ToyDuck extends Duck then it should also swim and does not complain that it can't swim, otherwise ToyDuck should not have extended Duck class.

share|improve this answer
    
It would be very constructive if people put comment also while down voting any answer. After all we are all here to share knowledge, and simply passing judgment without proper reason is not going to serve any purpose. –  AKS Aug 2 '13 at 11:36
    
this doesn't seem to offer anything substantial over points made and explained in prior 6 answers –  gnat May 12 at 14:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.