Skip to content

Instantly share code, notes, and snippets.

@tuanna-hsp
Last active December 15, 2021 04:13
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 tuanna-hsp/8be3c329c89ac095d932dbc77ce23d73 to your computer and use it in GitHub Desktop.
Save tuanna-hsp/8be3c329c89ac095d932dbc77ce23d73 to your computer and use it in GitHub Desktop.
Template methods
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