Sitemap
Amit Dhiman

Amit Dhiman

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Flutter, Angular, Javascript, Typescript, AWS

Strategy Design Pattern

2 min readJul 5, 2023

Let's discuss this in detail

The Strategy design pattern is a behavioral pattern that enables you to define a family of interchangeable algorithms or strategies, encapsulate each one, and make them interchangeable at runtime. It allows the algorithms to vary independently from the clients that use them.

Here’s an explanation of the Strategy pattern using a coding example in Kotlin:

// Strategy interface
interface SortingStrategy {
fun sort(data: MutableList<Int>)
}

// Concrete strategies
class BubbleSortStrategy : SortingStrategy {
override fun sort(data: MutableList<Int>) {
println("Using Bubble Sort")
// Perform bubble sort algorithm
// ...
println("Sorting completed.")
}
}

class QuickSortStrategy : SortingStrategy {
override fun sort(data: MutableList<Int>) {
println("Using Quick Sort")
// Perform quick sort algorithm
// ...
println("Sorting completed.")
}
}

// Context
class Sorter(private var sortingStrategy: SortingStrategy) {
fun setSortingStrategy(strategy: SortingStrategy) {
sortingStrategy = strategy
}

fun performSort(data: MutableList<Int>) {
sortingStrategy.sort(data)
}
}

// Client code
fun main() {
val data = mutableListOf(5, 3, 8, 1, 2)

val sorter = Sorter(BubbleSortStrategy())
sorter.performSort(data)

sorter.setSortingStrategy(QuickSortStrategy())
sorter.performSort(data)
}

In this example, we have a SortingStrategy interface that declares the contract for sorting algorithms. The BubbleSortStrategy and QuickSortStrategy classes implement this interface and provide their specific sorting algorithm implementations.

Get Amit Dhiman’s stories in your inbox

Join Medium for free to get updates from this writer.

The Sorter class acts as the context or the client that uses the sorting strategies. It has a setSortingStrategy method to dynamically change the sorting strategy at runtime. The performSort method invokes the sort method on the selected strategy to perform the sorting operation.

In the main function, we create an instance of Sorter with an initial sorting strategy (Bubble Sort in this case). We call the performSort method to perform sorting using the chosen strategy.

We can later change the sorting strategy dynamically by calling the setSortingStrategy method with a different strategy (Quick Sort in this case), and then call performSort again to perform sorting with the new strategy.

The Strategy pattern allows us to encapsulate the sorting algorithms (BubbleSortStrategy and QuickSortStrategy) independently of the Sorter class. The Sorter class is decoupled from the specific sorting algorithm implementation, and the algorithms can be easily added or changed without modifying the Sorter class.

This pattern promotes code reusability, flexibility, and maintainability, as different strategies can be used interchangeably based on specific requirements or conditions.

--

--

Amit Dhiman
Amit Dhiman

Written by Amit Dhiman

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Flutter, Angular, Javascript, Typescript, AWS