Strategy 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));
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?
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
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
Strategy Pattern
Glissez pour afficher le 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));
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?
Merci pour vos commentaires !