用设计模式替换多个If\Else [英] Replace Multiple If\Else with Design Pattern

查看:442
本文介绍了用设计模式替换多个If\Else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于SO上的各种线程(例如用模式替换if else语句),我知道我可以用 Command 模式替换多个if\else语句。

Based on various threads on SO (ex. Replacing if else statement with pattern ) I understand that I can replace multiple if\else statements with the Command pattern.

我的情况有些不同。

我有一系列 Commands ,只有前一个命令不成功时,我才需要执行每个 Command

I have a series of Commands and I need to execute each Command only if the previous command was unsuccessful.

例如,假设我需要从一个假设的网页中检索文本-我可以使用屏幕抓取直接从该网页中抓取文本,也可以从屏幕抓取中提取文本。 API。如果屏幕抓取不成功,我只想从API提取文本。换句话说,只有在 scrape命令不起作用的情况下,我才执行 fetch命令。

For example, suppose I need to retrieve text from a hypothetical web page - I could scrape the text directly from the page using screen scraping or I could fetch the text from the API. I would only want to fetch the text from API if screen scraping was not successful. In other words I would only execute the "fetch" command if the "scrape" command didn't work.

在这种情况下,我将测试刮取的String是否为空或等于null,然后执行第二个命令。如果第二个命令也不成功,我将执行第三个命令,依此类推。

In this case, I would test if the scraped String is empty or equal to null and then execute the second Command. If the second command is also unsuccessful I would execute the third Command and so on.

重要的是,只有在特定条件为true / false时,我们才执行后续命令。每个命令的条件始终相同,但是将来命令的数量可能会增加。

The important point is that we only execute subsequent commands if a certain condition is true/false. The condition is always the same for each Command but the number of commands might grow in the future.

我无法通过通常建议的路线(使用 Map Command 接口),因为如果以下情况将不会执行下一个 Command 第一个失败(它也无法检查 Command 是否成功)

I cannot implement this via the typically suggested route (using a Map and Command interface) bec this would not execute the next Command if the first one failed (it would also be unable to check if a Command was successful or not)

什么设计

推荐答案

人们建议使用责任链 code>模式,但是当我想到它发生的问题时,我可以使用 Command 模式的变体来解决此问题。

People have suggested using the chain of responsibility pattern however as I was thinking of the problem it occurred to me I could use a variation of the Command pattern to solve this problem. It works like this.

将每个 if 语句替换为实现的相应类命令界面。在我们的示例中,我将实现 ScreenScrapeTextCommand FetchTextFromAPICommand 类。由于每个类都将实现 Command 接口,我们将向每个类添加方法 execute()(该方法实际上将执行我们需要的功能,例如屏幕抓取或从API提取)。

Replace each if statement with a corresponding class which implements the Command interface. In our example, I would implement the classes ScreenScrapeTextCommand and FetchTextFromAPICommand. Since each class will implement the Command interface we will add the method execute() to each class (this method will actually execute the functionality we need such as screen scraping or fetching from API).

然后我们将创建一个包含所有相关命令的新类。我们将此类称为 GetTextCommandCenter 。该类将包含 list Commands 的列表,形式为 public List< Command>。 commandList; 然后,我们将以以下方式将前两个类添加到列表中,从而对命令进行硬编码:

We'd then create a new class which contains all related commands. Let's call this class GetTextCommandCenter. The class would contain a list of Commands in the form of public List<Command> commandList; We'd then hard code commands by adding the first two classes to the list in the following manner:

commandList.add(new ScreenScrapeTextCommand();
commandList.add(new FetchTextFromAPICommand();

然后我们可以将 if\else 链替换为以下内容:

We could then replace the if\else chain with the following:

public String replaceIfElseWithCommand()..

String text = null;
int commandIndex = 0;

while (text == null && text == someCondition)
{

text = new GetTextCommandCenter().commandList(commandIndex).execute();
commandIndex++;
}

return text;

}

命令成功后,while循环中的条件将失败,方法将相应返回。

As soon as a command is successful the the condition in the while loop will fail and method will return accordingly.

这样,我们可以通过向<$添加更多类来轻松添加更多命令c $ c> CommandCenter 类或更改ind的优先级我很快认为这是一个简单的命令。

This way, we can easily add more Commands by adding more classes to CommandCenter class or change the precedence of individual commands quickly.

我认为这是解决我的问题的简单明了的方法(但是如果您认为责任链,请发表评论模式更合适,以及您为什么这么认为)

I think this is a simple and straightforward solution to my problem (but please place a comment if you think the chain of responsibility pattern is more suitable and why you think so)

这篇关于用设计模式替换多个If\Else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆