DEV Community

Tirumal Rao
Tirumal Rao

Posted on

3

Strategy Pattern in Golang

The Strategy Pattern is a design pattern that allows selecting an implementation of an algorithm's interface at runtime. It allows clients to choose from various implementations of a particular algorithm without altering the code that uses the algorithm.

Example 1

package main

import (
    "fmt"
    "strings"
)

type TranformFunc func(s string) string

func UpperCase(s string) string {
    return strings.ToUpper(s)
}

func Prefixer(prefix string) TranformFunc {
    return func(s string) string {
        return prefix + s
    }
}

func transformString(s string, fn TranformFunc) string {
    return fn(s)
}

func main() {
    fmt.Println(transformString("hello sailor", Prefixer("FOO_")))
}
Enter fullscreen mode Exit fullscreen mode

Here's a breakdown of the code:

1. Strategy Interface:

  • TranformFunc is the function type that represents the strategy interface.

2. Concrete Strategies:

  • UpperCase: A strategy that converts a string to uppercase.
  • Prefixer: Returns another strategy that prefixes a string.

3. Context:

  • transformString: A function that represents the context. It accepts a string and a strategy and returns the string after applying the strategy.

4. Usage in main function:

  • In the main() function, we are using the Prefixer strategy to prefix "FOO_" to the string "hello sailor".

Let's see another example to understand the Strategy Pattern even better:

Example 2: Sorting Strategies
Suppose we want to sort a slice of integers, but we want to provide multiple sorting strategies.

package main

import (
    "fmt"
    "sort"
)

type SortStrategy func([]int) []int

func AscendingSort(nums []int) []int {
    sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] })
    return nums
}

func DescendingSort(nums []int) []int {
    sort.Slice(nums, func(i, j int) bool { return nums[i] > nums[j] })
    return nums
}

func sortNumbers(nums []int, strategy SortStrategy) []int {
    return strategy(nums)
}

func main() {
    numbers := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}

    fmt.Println("Original: ", numbers)
    fmt.Println("Ascending Sort:", sortNumbers(numbers, AscendingSort))
    fmt.Println("Descending Sort:", sortNumbers(numbers, DescendingSort))
}
Enter fullscreen mode Exit fullscreen mode

Here:

1. Strategy Interface:

  • SortStrategy is our strategy interface, which is a function type.

2. Concrete Strategies:

  • AscendingSort: Sorts a slice of integers in ascending order.
  • DescendingSort: Sorts a slice of integers in descending order.

3. Context:

  • sortNumbers: It accepts a slice of integers and a sorting strategy and returns the sorted slice. In the main() function, we have used both strategies to demonstrate the sorting in ascending and descending order.

Credits

First example taken from chapter Advanced interfaces and typed functions in fulltimegodev course by Anthony De Meulemeester

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more