400) -6ren">
gpt4 book ai didi

c# - 处理条件语句的最佳方法

转载 作者:太空宇宙 更新时间:2023-11-03 18:40:44 25 4
gpt4 key购买 nike

我在我的代码中使用了以下条件,但看起来效率不高,这是更好的处理方式吗?

if (ic = filename.Contains(".wmv"))
{
if (bitnumber > 400)
{
path = "ftp://" + ftpServerIP + "/" + "media" + "/" + "lib" + "/" + programName + "/" + date + "/";
UploadCondition(path, filename);
//return path;
}
}

if (ic = filename.Contains(".wmv"))
{
if (bitnumber < 400)
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
//return path;
}
}

if (ic = filename.Contains(".m4v"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}

if (ic = filename.Contains(".mp4"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
if (ic = filename.Contains(".flv"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
if (ic = filename.Contains(".mpg"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
if (ic = filename.Contains(".aac"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
if (ic = filename.Contains(".mp3"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}

最佳答案

在其他类中分解它,例如:

public class AudioFileValidator
{
private List<string> _extensions = new List<string>{".aac", ".mp3"};
public bool IsValid(string filename)
{
if (!_extensions.Contains(Path.GetExtension(filename))
return false;

//validate bitrate etc
}
}

用法:

var audioValidator = new AudioFileValidator();
if (audioValidator.IsValid(filename))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}

var videoValidator = new VideoFileValidator();
if (videoValidator.IsValid(filename))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}

通过这样做,您将获得具有单一职责的类,这些类可以在其他地方重用并且易于进行单元测试。


您甚至可以更进一步,引入一个名为 IMediaFileValidator 的新接口(interface),所有验证器都实现该接口(interface)。并做类似的事情:

foreach (var validator in validators)
{
if (validator.IsValid(filename))
{
// use info from the validator to build the path
var mediaName = validator.MediaName;
path = "ftp://" + ftpServerIP + "/" + mediaName + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
break;
}
}

这也会使您的代码遵守开放/封闭原则。

关于c# - 处理条件语句的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9288285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com