Using Typescript, I want to make a class of Builder of abstract class and follow a Builder Pattern and the child class will implement the details.
The parent abstract class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
export abstract class SQLQueryBuilder{ // private tableName: string; private configs: ConfigsHelper; readonly TABLE_PREFIX_CONFIG_NAME: string = "tablePrefix"; constructor(configs: HOption[]){ this.configs = new ConfigsHelper(this._getCompulsoryFields()); this.configs.loadFromHOption(configs); } protected getConfig(name: string): string{ return this.configs.get(name); } protected _getCompulsoryFields(): string[]{ return [this.TABLE_PREFIX_CONFIG_NAME]; } public abstract build(tableName: string): SQLQueryBuilder; }//class |
The child class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
export class KnexSQLQueryBuilder extends SQLQueryBuilder{ private knex = null; private query: Knex.QueryBuilder = null; constructor(configs: HOption[]){ super(configs); const knexConfigs = { client: super.getConfig("client"), connection: { host : super.getConfig("host"), user : super.getConfig("user"), password : super.getConfig("password"), database : super.getConfig("database") } }; this.knex = Knex(knexConfigs); } public build(tableName: string): KnexSQLQueryBuilder{ this.query = this.knex(tableName); return this; } }//class |
If you notice, you need to change the return of build() function into child class name.