# How is strategy pattern different from the open closed principle

**URL:** <https://forums.kodeco.com/t/how-is-strategy-pattern-different-from-the-open-closed-principle/66239>\
**Category:** Design Patterns by Tutorials\
**Created:** [April 2, 2019, 6:47am UTC](https://forums.kodeco.com/t/how-is-strategy-pattern-different-from-the-open-closed-principle/66239 "2019-04-02T06:47:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fuzzybuckbeak](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fuzzybuckbeak/32/85288_2.png) [@fuzzybuckbeak](https://forums.kodeco.com/u/fuzzybuckbeak)\
**Post date:** [April 2, 2019, 6:47am UTC](https://forums.kodeco.com/t/how-is-strategy-pattern-different-from-the-open-closed-principle/66239/1 "2019-04-02T06:47:34Z")

</div>

I have been reading the book on design patterns, thanks a lot for such a comprehensive book. However I have trouble understanding the difference between strategy pattern and open closed principle. I know one of it is a design pattern while the other is a principle, the examples are baffling me altogether. Could you please help me understand the difference between these?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 30, 2019, 1:06am UTC](https://forums.kodeco.com/t/how-is-strategy-pattern-different-from-the-open-closed-principle/66239/2 "2019-04-30T01:06:22Z")

</div>

@jrg.developer Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![jrg.developer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jrg.developer/32/109958_2.png) [@jrg.developer](https://forums.kodeco.com/u/jrg.developer)\
**Post date:** [May 1, 2019, 1:57pm UTC](https://forums.kodeco.com/t/how-is-strategy-pattern-different-from-the-open-closed-principle/66239/3 "2019-05-01T13:57:38Z")

</div>

> [@fuzzybuckbeak](#):
>
> Could you please help me understand the difference between [the strategy pattern and open closed principle]?

Great question! In case everyone isn’t familiar with the open-closed principle (OCP), let’s define it:

> The **open/closed principle** states “Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” - [Wikipedia](https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle)

That sounds complex! In simple terms, what does it mean for a class to be “open for extension, but closed for modification”?

It means you should be able to modify the behavior of a class without modifying its code.

When I first learned this, it seemed entirely contradictory– how can you modify how a class works without modifying its code? There are many ways actually!

The object-oriented programming way to do this is using inheritance: create a subclass and override any desired methods and properties. This works – you’re not modifying the code of the base class, so it satisfies OCP – but is it the _best_ option? It may not be!

For example, if you need to override the _same methods_ with the _same behavior_ in multiple subclasses, you’d wind up duplicating code if you only use inheritance.

Fortunately, there’s another option: Use the strategy pattern!

The strategy pattern encapsulates concrete behavior into a “strategy” object. You can then inject the strategy – pass an instance of the strategy via `init` or set it as a property – wherever you need it.

Does this satisfy the OCP? It sure does! Instead of changing behavior via subclassing, you depend on protocols. Whenever you need a new behavior, you create a new strategy that implements the protocol. This can save you a _lot_ of effort and code duplication!

Thereby, in summary:

- The open-closed principle is a guiding principle in software development that helps you write better code.
- The strategy pattern is one means of achieving the open-closed principle.

By the way, the OCP was made popular especially by Robert Martin’s book titled [Clean Code](https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882). I highly recommend reading it, if you haven’t already!

I hope this explanation helps. Let me know if you have any other questions about this. :]
