第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在繼承的類中處理不同類型的更好方法

在繼承的類中處理不同類型的更好方法

C#
富國(guó)滬深 2021-10-24 19:42:06
我正在使用 N 個(gè)提供商發(fā)送 SMS 消息的 Web 服務(wù)。每個(gè)提供者在自己的 Web 服務(wù)中接收不同格式的消息。為了在我的 Web 服務(wù)中創(chuàng)建模式,我創(chuàng)建了一個(gè)抽象類,其中包含要為每個(gè)提供程序?qū)崿F(xiàn)的許多方法。一些方法需要接收一個(gè)可以是不同類型的對(duì)象,這取決于繼承它的子類(提供者)。這些方法之一返回可以是至少兩種不同類型的對(duì)象。我使用“object”關(guān)鍵字實(shí)現(xiàn)了這一點(diǎn),它允許我傳遞或返回任何對(duì)象:public abstract class Provider {    protected HttpClient _Client;    protected SMSManagerAPIContext _Context;    public abstract DeliveryResponse SendSMS(object Sms);    protected abstract DeliveryResponse ParseResponse(HttpResponseMessage Response);    public abstract object PrepareMessage(SMS Sms);    protected abstract void SaveResponse(object Sms, HttpResponseMessage Response);}方法中Sms接收到的對(duì)象是SendSMS方法的返回PrepareMessage。問(wèn)題是PrepareMessage可能返回不同類型的對(duì)象。(例如,我的一個(gè)提供者接受發(fā)送一條消息或多條消息的請(qǐng)求,但每個(gè)消息的對(duì)象都不同。然后我在PrepareMessage方法中返回正確的對(duì)象,該SendSMS方法將其作為 JSON 對(duì)象發(fā)送給提供者。 )
查看完整描述

2 回答

?
慕斯709654

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

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-10-24
  • 2 回答
  • 0 關(guān)注
  • 196 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)