Search code examples
functional-programmingkdbstrategy-pattern

Strategy pattern in functional programming


I am trying to write a Strategy design pattern example in the functional programming language ( not purely functional, no objects, no function overloading) using the example mentioned on Java DZone.

Though I understand that a lot of functionality comes out of the box in the functional programming language.

Am I missing anything here in terms of the design pattern concept?

: is the assignment operator.

File FileCompressor

strategy:`noOp;

setCompressionAlgo:{[algo]
    strategy:algo
}

compressFiles:{[filesList]
    strategy[filesList]
 }

File ZipCompressor

zipCompress:{[fileList]
  //compress each file using the zip compression
 }

File RarCompressor

rarCompress:{[fileList]
  //compress each file using the rar compression
 }

File Client

start:{[path]
    filesList:getFiles[path];
    setCompressionAlgo[zipCompress];
    compressFiles[fileList]
 }

Solution

  • You usually wouldn't make the strategy a global (mutable) variable. You can simplify your example to

    start: {[path]
        compressFiles: zipCompress;
        // change to
        // compressFiles: rarCompress
        // to use a different strategy
    
        // apply the strategy:
        compressFiles[fileList1]
        compressFiles[fileList2]
    }