I'm trying to teach an object oriented design principles course (on SOLID) at a training institute. I also want to teach the students a few OOP design patterns such as factory, singleton and one other. I know there is no 1 to 1 match between SOLID principles and OOP Design patterns, but I want to introduce the students a pattern that is sort of inclusive of all the SOLID design principles at play.

Any ideas?

I'm really trying to fit in the observer pattern but want to keep it conforming with all the SOLID principles.

  • No. There is not any. Also, please. Don't teach them antipatterns like (plain)Factory and Singleton. Teach them Command, Visitor or Observer. – Euphoric Sep 5 '14 at 6:04
  • How is factory an anti pattern? – Tazo Man Sep 5 '14 at 6:05
  • 3
    Gnat this doesnt really have anything to do with career or education. Its a question on OOP patterns. Just because there is the term teaching in there doesnt make it fall in that list. That was just background info. – Tazo Man Sep 5 '14 at 7:04
  • 4
    @gnat: IMHO we should not close question as "asking for educational advice" just because the OP tells us the motivation for his (on-topic, programming) question is for educational purposes. – Doc Brown Sep 5 '14 at 7:56

One of the most popular design pattern among the community here is the strategy pattern. And yes, if you build some example code around this pattern, you can demonstrate all the SOLID principles:

  • S = is fulfilled when each strategy subclass is only responsible for exactly one task, and the "context" class does not take responsibilities which belong into the strategy classes
  • O = you can add new strategies afterwards ("open for extentions") without the need to change the internals of the context ("closed for modifications")
  • L = this means to implement the strategy subclasses correctly with the semantics defined by the strategy interface, not changing the semantics in any of the subclasses. LSP would not be fulfilled when a subclass implements the strategy interface in a manner which breaks the parts of the context using the strategy.
  • I = is fulfilled when the strategy base class offers only a small, single-purpose interface (like the one "execute" method)
  • D = the context relies completely on the abstract strategy interface, it gets the concrete strategy "injected" from outside (for example, at construction time) and does not make any assumptions about, for example, a fixed set of subclasses, or the availability of specific subclasses.

Note that the pattern itself does not guarantee your code to be SOLID, it is more that the SOLID principles help you to implement the pattern correctly. You may consider to show your students not only correct examples for applying the SOLID principles, but also counter-examples in context of a strategy pattern, showing code which breaks each of the five principles. Or even better: make this an exercise for them.


SOLID principles: It’s an acronym of five principles.

| Initial | Stands for |            Full form            |
| S       | SRP        | Single responsibility principle |
| O       | OCP        | Open/closed principle           |
| L       | LSP        | Liskov substitution principle   |
| I       | ISP        | Interface segregation principle |
| D       | DIP        | Dependency inversion principle  |

Single responsibility principle: A class should have only a single responsibility.

  • Analogy: I work as a team leader for one of the software firms in India. In my spare time I do some writing, newspaper editing and other various projects. Basically, I have multiple responsibilities in my life.

    When something bad happens at my work place, like when my boss scolds me for some mistake, I get distracted from my other work. Basically, if one thing goes bad, everything will mess up.

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

  • Analogy: Let’s assume you want to add one more floor between the first and second floor in your two floor house. Do you think it is possible? Yes it is, but is it feasible? Here are some options:

    • One thing you could have done at time you were building the house first time was make it with three floors, keeping second floor empty. Then utilize the second floor anytime you want. I don’t know how feasible that is, but it is one solution.
    • Break the current second floor and build two new floors, which is not sensible.

Liskov Substitution principle: Subtypes must be substitutable for their base types. Note: S is already occupied in SOLID so pre-fixed by Liskov author name.

  • Analogy: Ostrich is a Bird (definitely it is!) and hence it inherits the Bird class. Now, can it fly? No! Here, the design violates the LSP.

    So, even if in real world this seems natural, in the class design, Ostrich should not inherit the Bird class, and there should be a separate class for birds that can't really fly and Ostrich should inherit that.

Interface segregation principle: Clients should not be forced to implement interfaces they don’t use.

  • Analogy: Imagine a situation where the shopkeeper starts showing you the ball and stumps as well. It may be possible that we will get confused and may end up buying something we did not require. We may even forget why we were there in the first place.

Dependency inversion principle: High level modules should not depend upon low level modules. Rather, both should depend upon abstractions.

  • Analogy: Let’s talk about our desktop computers. Different parts such as RAM, a hard disk, and CD-ROM (etc.) are loosely connected to the motherboard. That means that, if, in future in any part stops working it can easily be replaced with a new one. Just imagine a situation where all parts were tightly coupled to each other, which means it would not be possible to remove any part from the motherboard. Then in that case if the RAM stops working we have to buy new motherboard which is going to be very expensive.

source1 source2

  • OP did not ask for definitions. – Basilevs May 26 '18 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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