From the course: Java Design Patterns: Behavioral Part 2

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Understanding the Strategy pattern

Understanding the Strategy pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 2

Start my 1-month free trial

Understanding the Strategy pattern

- [Instructor] Say you have a group of algorithms and you want to choose which one to use dynamically at runtime. In the strategy pattern, the word strategy usually refers to an algorithm, and it allows you to do just this, to define a family of algorithms and make them interchangeable. As a real life example, say you have a video game, and the person you're controlling has different powers they can use. For example, they can fly, they can climb things, they can swim, and so on. The action the player takes or the strategy they use will depend on the situation they find themselves in during the game. They'll need to decide dynamically which one to use. This is a bit like how the strategy pattern works. In Java, there are many different scenarios where you might use the strategy pattern. For example, if you want to encrypt files, but have different ways of encrypting them depending on the situation, or if you have…

Contents