Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Strategy Pattern | Behavioral Patterns
Quizzes & Challenges
Quizzes
Challenges
/
JavaScript Design Patterns

bookStrategy Pattern

The Strategy pattern is a behavioral design pattern that enables you to define a family of algorithms, encapsulate each one, and make them interchangeable. Instead of hardcoding a specific algorithm directly into your code, you can select the desired algorithm at runtime. This is especially useful when you want to allow your code to choose between multiple ways of performing a task, such as sorting, filtering, or formatting data.

In JavaScript, the Strategy pattern often involves passing functions or objects that implement a specific interface or contract. This allows you to change the behavior of an operation without modifying the code that uses it.

123456789101112131415161718192021222324252627282930313233
// Define different sorting strategies function bubbleSort(arr) { // A simple (but inefficient) sorting algorithm let a = [...arr]; for (let i = 0; i < a.length; i++) { for (let j = 0; j < a.length - i - 1; j++) { if (a[j] > a[j + 1]) { [a[j], a[j + 1]] = [a[j + 1], a[j]]; } } } return a; } function quickSort(arr) { // A simple recursive quicksort if (arr.length < 2) return arr; const pivot = arr[0]; const less = arr.slice(1).filter(x => x <= pivot); const greater = arr.slice(1).filter(x => x > pivot); return [...quickSort(less), pivot, ...quickSort(greater)]; } // The function that accepts a strategy function sortArray(arr, strategy) { return strategy(arr); } // Usage const numbers = [5, 2, 9, 1, 5, 6]; console.log('Bubble sort:', sortArray(numbers, bubbleSort)); console.log('Quick sort:', sortArray(numbers, quickSort));
copy

By using the Strategy pattern, you can easily switch between different algorithms without changing the code that relies on them. This increases your code's flexibility and maintainability. If you need to add a new sorting method, you simply define a new strategy function and pass it to sortArray. This approach helps keep your code open for extension but closed for modification, which is a key principle of robust software design.

1. The Strategy pattern allows you to:

2. Why is the Strategy pattern useful?

question mark

The Strategy pattern allows you to:

Select the correct answer

question mark

Why is the Strategy pattern useful?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain how the Strategy pattern improves code maintainability?

What are some other real-world examples where the Strategy pattern can be useful?

How would I add a new sorting algorithm using this pattern?

Awesome!

Completion rate improved to 7.14

bookStrategy Pattern

Deslize para mostrar o menu

The Strategy pattern is a behavioral design pattern that enables you to define a family of algorithms, encapsulate each one, and make them interchangeable. Instead of hardcoding a specific algorithm directly into your code, you can select the desired algorithm at runtime. This is especially useful when you want to allow your code to choose between multiple ways of performing a task, such as sorting, filtering, or formatting data.

In JavaScript, the Strategy pattern often involves passing functions or objects that implement a specific interface or contract. This allows you to change the behavior of an operation without modifying the code that uses it.

123456789101112131415161718192021222324252627282930313233
// Define different sorting strategies function bubbleSort(arr) { // A simple (but inefficient) sorting algorithm let a = [...arr]; for (let i = 0; i < a.length; i++) { for (let j = 0; j < a.length - i - 1; j++) { if (a[j] > a[j + 1]) { [a[j], a[j + 1]] = [a[j + 1], a[j]]; } } } return a; } function quickSort(arr) { // A simple recursive quicksort if (arr.length < 2) return arr; const pivot = arr[0]; const less = arr.slice(1).filter(x => x <= pivot); const greater = arr.slice(1).filter(x => x > pivot); return [...quickSort(less), pivot, ...quickSort(greater)]; } // The function that accepts a strategy function sortArray(arr, strategy) { return strategy(arr); } // Usage const numbers = [5, 2, 9, 1, 5, 6]; console.log('Bubble sort:', sortArray(numbers, bubbleSort)); console.log('Quick sort:', sortArray(numbers, quickSort));
copy

By using the Strategy pattern, you can easily switch between different algorithms without changing the code that relies on them. This increases your code's flexibility and maintainability. If you need to add a new sorting method, you simply define a new strategy function and pass it to sortArray. This approach helps keep your code open for extension but closed for modification, which is a key principle of robust software design.

1. The Strategy pattern allows you to:

2. Why is the Strategy pattern useful?

question mark

The Strategy pattern allows you to:

Select the correct answer

question mark

Why is the Strategy pattern useful?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
some-alt