为不同服务提供商访问 api 实施什么设计模式

What design pattern to implement for accessing api for different service providers

我想在 rails 上的 ruby 中添加一个功能(更像是 gem),用户可以在其中指定他们的服务提供商名称,然后它会自动获取相关配置并将其注入模型文件并创建必要的迁移。

我的问题是如何实现这一目标。我的意思是我应该使用哪种设计模式,以便每当用户指定其提供商名称(例如 Exotel、Sinch、Twilio 等)时,它会将相应的配置注入配置文件及其 api 设置以发送电子邮件和消息.

我已经检查过这个,但它似乎并没有解决我的问题。

例如rails 的 Exotel api 配置是 -

Exotel.configure do |c|
  c.exotel_sid   = "Your exotel sid"
  c.exotel_token = "Your exotel token"
end

发送消息

response = Exotel::Sms.send(:from => 'FROM_NUMBER', :to => 'TO_NUMBER', :body => 'MESSAGE BODY')
sms_id = response.sid #sid is used to find the delivery status and other details of the message in future.

发送消息的 sinch 配置是 -

SinchSms.send('YOUR_APP_KEY', 'YOUR_APP_SECRET', "Your code is #{code}", phone_number)
render status: 200, nothing: true

现在我希望 gem 根据用户输入的服务提供商名称来完成所有这些工作,即,如果用户输入 Exotel,那么 gem 将设置 exotel 设置,如果它是 Sinch,则不是 sinch 的将设置配置。

听起来您想要适配器模式。 https://en.wikipedia.org/wiki/Adapter_pattern or maybe an api gateway http://microservices.io/patterns/apigateway.html