I need to create a instance based on a query.
I have an interface like this:
import { GatewaySendMessageDto } from "./dto/gateway-send-message.dto";
import { IGateway } from "./interfaces/IGateway";
export interface GatewayInterface {
sendMessage(channel: IGateway, data: GatewaySendMessageDto):void
}
and it's the basic method that my classes needs to implement
I create the Factory Class like this
export class GatewayFactory {
public GatewayFactory(){}
public getGateway (type) {
let a = GatewayTypesEnum.Telegram;
let gateway:GatewayInterface = null;
switch (type){
case 'Telegram':
gateway = new TelegramService(??);
break;
case GatewayTypesEnum.Discord:
//gateway = new foo();
break;
}
return gateway;
}
}
I need to create an instance of telegramService when the type is 'Telegram', but it has a dependecy like:
constructor(
@InjectModel(Gateway.name) private readonly gatewayModel: Model<Gateway>,
private bot: Telegraf,
private imagesService: ImagesService,
)
obviously, telegramService implements GatewayInterface
so in my GatewayService I could do:
async sendMessageToGatway(){
let gateway = new GatewayFactory()
return gateway.getGateway('Telegram').sendMessage();
}
How can I implement the Factory Pattern method when the service has the dependencies? Thanks
Aucun commentaire:
Enregistrer un commentaire