I had an exam today on the subject of design patterns. There was one question I didn't know the answer to and I'm very curious.
True or false
"The open/closed principle is the key to the strategy pattern".
I asked the professor what he meant by "the key to" and clarified: "Do you still have the strategy pattern without the OCP?"
Do you guys have an idea?
I hated these types of questions back in college because I have always felt they are opinionated.
Nevertheless, if I have to give a single answer I would say False.
Why ?
I think OCP is a general principle that applies at many levels ranging from small refactorings in your code to architectural patterns and it is not restricted to design patterns only.
OCP advises you to keep your code open for extension and closed for modification, certainly the Strategy pattern applies this as you can add more strategies at run-time and in the future without having to modify your code but this doesn't mean that Strategy wouldn't have existed without the OCP.
Actually the think that made me say False is that I think the key to the Strategy pattern is principle: Program to Interface Not to an Implementation, even in college we learned them in that exact order.