理解开放封闭原则
当我遇到以下代码时,我正在重构 一个简单的脚本文件解析器的旧代码:
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, // ie 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, ie add the "line" // to the current scope. scope.Append(line); break; } line = reader.ReadLine(); }
通过应用“开放封闭原则”,这个简单的脚本处理器在我看来是一个很好的重构候选者。 以$
开头的行可能永远不会以不同的方式处理。 但是,如果以指令开头的新指令怎么办!
需要添加? 或者需要新的处理标识符(例如新的交换机箱)?
问题是,我无法弄清楚如何在不破坏OCP的情况下轻松正确地添加更多指令和处理器。 的!
-case使用scope
和/或 line
使得它有点棘手, default
情况也是如此。
有什么建议?
使用Dictionary
指定字符的处理方式。 如果字典中不存在字符键,则调用DefaultHandler
。
添加Add(char key, YourDelegate handler)
方法,允许任何人处理特定字符。
更新
最好使用接口:
/// /// Let anyone implement this interface. /// public interface IMyHandler { void Process(IProcessContext context, string line); } /// /// Context information /// public interface IProcessContext { } // Actual parser public class Parser { private Dictionary _handlers = new Dictionary(); 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
。 它提供了更大的灵活性,因为源可以是从简单字符串到复杂流的任何内容。
更新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` } }
这为处理实际协议和添加更多命令提供了更大的灵活性。 您无需更改任何内容即可添加更多function。 因此,对于Open / Closed和其他一些SOLID原则,该解决方案是可行的。
上述就是C#学习教程:理解开放封闭原则分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/942269.html