工作场景:编写一个给用户发送短信的程序
传统写法
#include <stdio.h>
#include <string>
enum NotifMessageType
{
WUDI,
FEITIAN
};
class NotifMessage
{
public:
int sendMessage(long long userId, std::string& msg, NotifMessageType type)
{
std::string phoneNumber = "123666666";
if (type == WUDI) {
// 代发短信公司的代码接口
}
else if(type == FEITIAN) {
// 另一个代发短信公司的代码接口
}
return 0;
}
};
int main()
{
return 0;
}
-
int sendMessage()
一个向用户发送信息的接口,需要知道用户的id,发送内容,发送短信是否成功的状态码来返回给int -
enum NotifMessageType
利用枚举类型根据条件判断判断是哪个公司的代码接口
标签:NotifMessageType,int,接口,工厂,WUDI,模式,FEITIAN,type From: https://www.cnblogs.com/Gal0721/p/17725644.html像上述如果根据情况需要接入的接口越来越多,需要写的if else语句越来越冗长,该怎么解决呢