Last active
December 15, 2021 04:13
-
-
Save tuanna-hsp/8be3c329c89ac095d932dbc77ce23d73 to your computer and use it in GitHub Desktop.
Template methods
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
abstract class AbstractAggregationClient { | |
constructor(private readonly options) {} | |
// | |
// Public interface | |
// | |
public getAccounts(params): AggregationResult { | |
try { | |
// Preparation step | |
this.beforeAggregation(params); | |
// Actual execution | |
return this.getAccountsImpl(params); | |
} catch (e) { | |
// Error handling | |
return this.onError(params, e); | |
} finally { | |
// Cleanup step | |
this.afterAggregation(params); | |
} | |
} | |
// | |
// Template methods | |
// | |
protected getAccountsImpl(params): AggregationResult { | |
// Let the child classes override this method | |
} | |
protected beforeAggregation(params): void { | |
// Do something. Child classes can override this method | |
} | |
protected afterAggregation(params): void { | |
// Do something. Child classes can override this method | |
} | |
protected onError(params, error): AggregationResult { | |
// Default error handling. Child classes can override this method | |
this.logger.error(error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment