2 回答

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
使用工廠。使用它的代碼可能只關(guān)心向某個(gè)電話號(hào)碼提供字符串消息,并返回是否成功以及如果不成功則可能返回錯(cuò)誤消息。
創(chuàng)建消息本身以遵循不同的提供者/實(shí)現(xiàn)應(yīng)該只是在一個(gè)黑匣子中。不同的提供者只會(huì)有不同的構(gòu)造函數(shù)。
public interface ISmsProvider
{
(bool, string) SendMessage(string number, string message);
}
public class SampleSmsProvider1 : ISmsProvider
{
public SampleSmsProvider1(string userKey, string passKey)
{
// initialize
}
public (bool, string) SendMessage(string number, string message)
{
// send the message (using a provider implementation from NuGet perhaps)
// return success/fail and error message, if applicable
return (true, string.Empty);
}
}
public class SmsFactory
{
public ISmsProvider GetProvider()
{
// Initialize and return one of the ISmsProvider implementations, depending on (I guess) the configuration
}
}
- 2 回答
- 0 關(guān)注
- 196 瀏覽
添加回答
舉報(bào)