我想开发一个通过串口与电路通信的库。该电路理解大约100个串行命令并返回响应字符串。
命令分为3个类别(设置,环境和输出),因此我想使用属性或其他方法在整个类中嵌套类别和方法。不是将所有命令方法都放在一个类下,而是建议嵌套它们的方法是什么?我想不惜一切代价避免以下情况,这只会是一团糟:
public class CircuitLib
{
...
// Methods.
public string SettingCommand1(string command) {...}
...
public string SettingCommand30(string command) {...}
public string EnvironmentCommand1(string command) {...}
...
public string EnvironmentCommand30(string command) {...}
public string OutputCommand1(string command) {...}
...
public string OutputCommand30(string command) {...}
}
能够使用属性到达指定的类别会很好:
circuitLibInstance.GetSettingsProperty.OneOfTheSettingsMethods(stringCommand);
任何指导都会非常感激。
谢谢大家!
答案 0 :(得分:3)
你可以拥有:
public class Circuit
{
// Initialize these in the constructor
private readonly SettingCommands settings;
private readonly EnvironmentCommands environment;
private readonly OutputCommands output;
public SettingCommands Settings { get { return settings; } }
public EnvironmentCommands Environment { get { return environment; } }
public OutputCommands Output { get { return output; } }
}
public class SettingCommands
{
public string Foo(string command) { ... }
}
然后:
string result = instance.Settings.Foo("hello");
那是你所追求的吗?
当然,如果所有命令方法实际上具有相同的结构,则可能需要枚举,例如:
public enum SettingCommand { Foo, Bar, Baz }
public enum EnvironmentCommand { Alice, Bob, Charlie }
public enum OutputCommand { Ender, Bean, Alai }
然后:
public class Circuit
{
public string Execute(SettingCommand command, string data) { ... }
public string Execute(EnvironmentCommand command, string data) { ... }
public string Execute(OutputCommand command, string data) { ... }
}
并用作:
string result = instance.Execute(SettingCommand.Foo, "data");
答案 1 :(得分:0)
我猜测命令字符串必须以非常具体的方式进行格式化。如果是这样的话,我会抽象出这些知识,以便它被封装在你的CircuitLib中。所以我可能会有类似的东西:
public class CircuitLib {
public string SettingCommand1(int a, bool b, string c) {}
public string SettingCommand2(bool a, double b) {}
public string EnvironmentCommand2(string a, DateTime b) {}
...
}
各个方法负责接受强类型数据并创建发送到设备的属性格式化字符串。在这种情况下,我没有看到有100个方法的类有任何问题,因为它封装了一个具有非常过程的接口的设备,可以理解100种方法。
或者,如果你真的想要你建议的界面,你可以很容易地做到这一点:
public class CircuitLib {
public SettingCircuitLib Settings {get; set;}
public EnvironmentCircuitLib Environment {get; set;}
}
public class SettingCircuitLib {
public string SettingCommand1(string command) {}
public string SettingCommand2(string command) {}
}
然后你就这样称呼它:
CircuitLibInstance.Settings.SettingCommand1(cmd);
CircuitLibInstance.Environment.EnvironmentCommand1(cmd);
答案 2 :(得分:0)
我想我可能会更加“流利”地接近它:
public class CircuitLib
{
public CircuitSetting Setting { get; }
public CircuitEnvironment Environment { get; }
public CircuitOutput Command { get; }
}
//Obviously you need one for each type above
public class CircuitSetting
{
CircutSettingCommand1 Command1 { get; }
CircutSettingCommand30 Command30 { get; }
}
public class CircutSettingCommand1
{
Send(string command);
}
然而,你构建类结构,如上所述,或某些接口等,目标仍然是相同的。能够编写类似以下内容的客户端代码:
using(CircuitLib lib = new CircuitLib(...))
{
lib.Setting.Command1.Send("Hello World");
}
这使得用户在向下导航时可以选择更少的选择。
答案 3 :(得分:0)
我会将您的库创建为一个包含三个实例类的静态类,如:
public class Settings
{
public string SettingCommand1(string command) {...}
...
public string SettingCommand30(string command) {...}
}
public class Environment
{
public string EnvironmentCommand1(string command) {...}
...
public string EnvironmentCommand30(string command) {...}
}
public class Output
{
public string OutputCommand1(string command) {...}
...
public string OutputCommand30(string command) {...}
}
public static class CircuitLib
{
public static Settings Settings = new Settings ();
public static Environment Environment = new Environment ();
public static Output Output = new Output();
}
然后你就可以使用它:
CircuitLib.Settings.SettingCommand1("input");
CircuitLib.Environment.EnvironmentCommand30("input");