Strategy Pattern for Powershell Functions

In PowerShell, as far as I know, code is usually function based rather than object based.

A nice design pattern, is the Strategy Design pattern, which allows to dynamically swap out a different concrete strategy, to solve a particular problem.

A way I found to implement strategy pattern, at the functional level, is to invoke a Powershell script by name.

If we have 3 scripts, that each are variants of the same algorithm (each script implements a concrete strategy),
then we can dynamicallly select which concrete variant to use, by setting a variable name to the appropraite script name.

Some clever folks have already figured out, how to execute a Powershell script, from a variable name:

http://stackoverflow.com/questions/2269174/powershell-dynamic-function-call


I am sure there is an OO way to achieve this in Powershell, however for those of us in a hurry, or working with more functional style Powershell, then this is a convenient way to reuse code.

Comments