Tags

  • AWS (7)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (15)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • MathHistory (1)
  • MySQL (21)
  • Neovim (10)
  • Network (66)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (3)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    [Behavioral] Strategy

    Published Apr 21, 2022 [  DesignPattern  ]

    Real world example

    Consider the example of sorting, we implemented bubble sort but the data started to grow and bubble sort started getting very slow. In order to tackle this we implemented Quick sort. But now although the quick sort algorithm was doing better for large datasets, it was very slow for smaller datasets. In order to handle this we implemented a strategy where for small datasets, bubble sort will be used and for larger, quick sort.

    In plain words

    Strategy pattern allows you to switch the algorithm or strategy based upon the situation.

    Wikipedia says

    In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables an algorithm’s behavior to be selected at runtime.

    Programmatic example

    Translating our example from above. First of all we have our strategy interface and different strategy implementations

    interface SortStrategy {
        sort(dataset: number[]): number[];
    }
    
    class BubbleSortStrategy implements SortStrategy {
        sort(dataset: number[]): number[] {
            console.log('Sorting using bubble sort')
    
            return dataset
        }
    }
    
    class QuickSortStrategy implements SortStrategy {
        sort(database: number[]): number[] {
            console.log('Sorting using quick sort')
            return database
        }
    }
    

    And then we have our client that is going to use any strategy

    class Sorter {
        protected sorter;
    
        constructor(sorter: SortStrategy) {
            this.sorter = sorter
        }
    
        sort(dataset: number[]): number[] {
            return this.sorter.sort(dataset)
        }
    }
    

    And it can be used as

    test('test strategy', () => {
        const dataset = [1, 5, 4, 3, 2, 8]
    
        let sorter = new Sorter(new BubbleSortStrategy())
        sorter.sort(dataset)
    
        sorter = new Sorter(new QuickSortStrategy())
        sorter.sort(dataset)
    })
    

    References