Skip to content

Instantly share code, notes, and snippets.

@matthewstokeley
Last active April 7, 2020 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewstokeley/4c0e1ce8346454cd3ca9043d9902d970 to your computer and use it in GitHub Desktop.
Save matthewstokeley/4c0e1ce8346454cd3ca9043d9902d970 to your computer and use it in GitHub Desktop.
Delegation design pattern
// #### Delegation
// A SOLID method is delegated via composition.
/**
*
*/
type Subtask: Function(name: String): Function
/**
*
*/
type Delegator: Function(task: Subtask): Function
( function() {
let prop = 'value'
// expressions
let fn1: Subtask = function(
name: String
): Function {
return function(prefix: String) {
return `${prefix}-${prop}`
}
}
let fn2: Delegator = function(
task: Subtask
): Function {
return function(...args) {
// other work
// ...
// delegation
task.apply(this, args)
}
}
} )( )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment