试图用工厂方法摆脱很多 if else

Trying to get rid of many if else with factory method

    if (ftpType == "YouTube")
    {
        var digitalServiceResponse = JsonConvert.DeserializeObject<YoutubeMetadataResponse>(metadataJson);

        var fileRequest = new AddDeliveryFileRequest
        {
            //some code
        };

        ftpDeliveryQueue.AddFile(fileRequest);
    }
    else if (ftpType == "Flashtalking")
    {
        var metadata = JsonConvert.DeserializeObject<FlashtalkingMetadataResponse>(metadataJson);

        foreach (var file in metadata.FileList)
        {
            var fileRequest = new AddDeliveryFileRequest
            {
                //some code
            };

            ftpDeliveryQueue.AddFile(fileRequest);
        }
    }
    else if (ftpType == "Innovid")
    {
        var metadata = JsonConvert.DeserializeObject<InnovidMetadataResponse>(metadataJson);

        foreach (var file in metadata.FileList)
        {
            var fileRequest = new AddDeliveryFileRequest
            {
                //some code
            };

            ftpDeliveryQueue.AddFile(fileRequest);
        }
    }
    else
    {
        var digitalServiceResponse = JsonConvert.DeserializeObject<SizmekMetadataResponse>(metadataJson);

        foreach (var file in digitalServiceResponse.Metadata.FileList)
        {
            var fileRequest = new AddDeliveryFileRequest
            {
                //some code
            };
            ftpDeliveryQueue.AddFile(fileRequest);
        }
    }

如何借助工厂方法摆脱这些 if 和 else if 块?

您可以使用 Dictionary,其中键是 string,值是 Action

例如:

static void Main( string[ ] args )
{ 
    // Obviously replace the body of these lambdas with your 
    // custom logic or have them call a private member method.
    var dict = new Dictionary<string, Action>
    {
        { "YouTube", ( ) => { Console.WriteLine( "YouTube" ); } },
        { "Flashtalking", ( ) => { Console.WriteLine( "Flashtalking" ); } },
        { "Innvoid", ( ) => { Console.WriteLine( "innvoid" ); } },
    };
    
    // This is what this would look like if you wanted to accept
    // parameters. In this case, each `Action` would have to accept
    // the same number of parameters and parameter types.
    var dict2 = new Dictionary<string, Action<int, string>>
    {
        { "YouTube", ( num, str ) => { Console.WriteLine( "YouTube" ); } },
        { "Flashtalking", ( num, str ) => { Console.WriteLine( "Flashtalking" ); } },
        { "Innvoid", ( num, str ) => { Console.WriteLine( "innvoid" ); } },
    };
    
    // If you want to pass an arbitrary number of parameters
    // and types you could pass an array of objects and just
    // cast the elements to their known types.
    var dict3 = new Dictionary<string, Action<object[ ]>>
    {
        { "YouTube", ( objects ) => { Console.WriteLine( $"YouTube: {( int )objects[ 0 ]}" ); } },
        { "Flashtalking", ( objects ) => { Console.WriteLine( $"Flashtalking: {objects[ 1 ]}" ); } },
        { "Innvoid", ( _ ) => { Console.WriteLine( "innvoid" ); } },
    };

    // Then you can use it like this.
    var key = "YouTube";
    if ( dict.ContainsKey( key ) )
        dict[ key ]( );

    // Or to pass parameters.
    if ( dict2.ContainsKey( key ) )
        dict2[ key ]( 42, "Some string" );

    // Or to pass an arbitrary number of parameters and types of
    // of parameters.
    if ( dict3.ContainsKey( key ) )
        dict3[ key ]( new object[ ] { 42, "Some string" } );

}

您当然希望将 Dictionary 设为封装 classreadonly 字段。