12

当我遇到以下代码时, 我正在重构一个简单的脚本文件解析器的一些旧代码:

StringReader reader = new StringReader(scriptTextToProcess);
StringBuilder scope = new StringBuilder();
string line = reader.ReadLine();
while (line != null)
{
    switch (line[0])
    {
        case '$':
            // Process the entire "line" as a variable, 
            // i.e. add it to a collection of KeyValuePair.
            AddToVariables(line);
            break;
        case '!':
            // Depending of what comes after the '!' character, 
            // process the entire "scope" and/or the command in "line".
            if (line == "!execute")
                ExecuteScope(scope);
            else if (line.StartsWith("!custom_command"))
                RunCustomCommand(line, scope);
            else if (line == "!single_line_directive")
                ProcessDirective(line);

            scope = new StringBuilder();
            break;

        default:
            // No processing directive, i.e. add the "line" 
            // to the current scope.
            scope.Append(line);
            break;
    }

    line = reader.ReadLine();
}

这个简单的脚本处理器在我看来是通过应用“开放封闭原则”进行重构的一个很好的候选者。以 a 开头的行$可能永远不会有不同的处理方式。但是,如果!需要添加以 a 开头的新指令怎么办?还是需要新的处理标识符(例如新的 switch-case)?

问题是,我无法弄清楚如何在不破坏 OCP 的情况下轻松正确地添加更多指令和处理器。!使用scope 和/或 的-caseline使它有点棘手,就像default-case 一样。

有什么建议么?

4

1 回答 1

24

使用 aDictionary<Char, YourDelegate>指定应如何处理字符。DefaultHandler如果字典中不存在字符键,则调用。

添加一个Add(char key, YourDelegate handler)允许任何人处理特定字符的方法。

更新

最好使用接口:

/// <summary>
/// Let anyone implement this interface.
/// </summary>
public interface IMyHandler
{
    void Process(IProcessContext context, string line);
}

/// <summary>
/// Context information
/// </summary>
public interface IProcessContext
{
}


// Actual parser
public class Parser
{
    private Dictionary<char, IMyHandler> _handlers = new Dictionary<char, IMyHandler>();
    private IMyHandler _defaultHandler;

    public void Add(char controlCharacter, IMyHandler handler)
    {
        _handlers.Add(controlCharacter, handler);
    }

    private void Parse(TextReader reader)
    {
        StringBuilder scope = new StringBuilder();
        IProcessContext context = null; // create your context here.

        string line = reader.ReadLine();
        while (line != null)
        {
            IMyHandler handler = null;
            if (!_handlers.TryGetValue(line[0], out handler))
                handler = _defaultHandler;

            handler.Process(context, line);


            line = reader.ReadLine();
        }
    }
}

请注意,我TextReader改为传入 a 。它提供了更大的灵活性,因为源可以是从简单字符串到复杂流的任何内容。

更新 2

我也会!以类似的方式分解处理。即创建一个处理IMyHandler的类:

public interface ICommandHandler
{
    void Handle(ICommandContext context, string commandName, string[] arguments);
}

public class CommandService : IMyHandler
{
    public void Add(string commandName, ICommandHandler handler) 
    {
    }

    public void Handle(IProcessContext context, string line)
    {
       // first word on the line is the command, all other words are arguments.
       // split the string properly

       // then find the corrext command handler and invoke it.
       // take the result and add it to the `IProcessContext`
    }
}

这为处理实际协议和添加更多命令提供了更大的灵活性。您无需更改任何内容即可添加更多功能。因此,对于 Open/Closed 和其他一些 SOLID 原则,该解决方案是可以的。

于 2011-03-24T08:31:40.193 回答