Last active
April 7, 2020 13:34
-
-
Save matthewstokeley/4c0e1ce8346454cd3ca9043d9902d970 to your computer and use it in GitHub Desktop.
Delegation design pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #### 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