我有一个控制电路,它有多种设置,可以连接任意数量的传感器(每个都有自己的设置).这些传感器只能与控制电路一起使用.我想过使用嵌套类,如下所示:
public class ControlCircuitLib
{
// Fields.
private Settings controllerSettings;
private List<Sensor> attachedSensors;
// Properties.
public Settings ControllerSettings
{ get { return this.controllerSettings; } }
public List<Sensor> AttachedSensors
{ get { return this.attachedSensors; } }
// Constructors, methods, etc.
...
// Nested classes.
public class Settings
{
// Fields.
private ControlCircuitLib controllerCircuit;
private SerialPort controllerSerialPort;
private int activeOutputs;
... (many, many more settings)
// Properties.
public int ActiveOutputs
{ get { return this.activeOutputs; } }
... (the other Get properties for the settings)
// Methods.
... (method to set the circuit properties though serial port)
}
public class Sensor
{
// Enumerations.
public enum MeasurementTypes { Displacement, Velocity, Acceleration };
// Fields.
private ControlCircuitLib controllerCircuit;
private string sensorName;
private MeasurementTypes measurementType;
private double requiredInputVoltage;
... (many, many more settings)
// Properties.
public string SensorName {...}
... (Get properties)
// Methods.
... (methods to set the sensor settings while attached to the control circuit)
}
}
Run Code Online (Sandbox Code Playgroud)
我已经读过,公共嵌套类是"禁忌",但有例外.这个结构还可以,还是有更好的选择?
谢谢!
编辑
下面是我试图为其编写库类的控制电路的原始层次结构; 我使用代码格式来防止文本换行.
Control Circuit (com. via serial port) -> Attached Sensors (up to 10) -> Sensor Settings (approx. 10 settings per sensor)
Basic Controller Settings (approx. 20 settings)
Output Settings (approx. 30 settings)
Common Settings (approx. 30 settings)
Environment Settings (approx. 10 settings)
Run Code Online (Sandbox Code Playgroud)
所有设置都是通过控制器设置的,但我想要一个有组织的库,而不是只在一个Controller类下填充所有~100个方法,属性和设置.如果有人可以提供一个简短的例子来概述他们将使用的结构,那将是非常感激的.谢谢!
Eri*_*ert 23
一类的内容应该是实现细节是的类.嵌套类是外部类的实现细节,还是仅仅使用外部类作为方便的名称范围和发现机制?
如果是前者,那么您不应该公开提供私有实现细节.如果它们是类的实现细节,则将它们设为私有.
如果是后者,那么你应该使用命名空间而不是外部类作为你的范围和发现机制.
无论哪种方式,公共嵌套类都是一个糟糕的代码味道.我想有一个很好的理由来公开一个嵌套类.
我对公共嵌套类没有太多问题(一般来说我不是教条规则的粉丝)但是你考虑过将所有这些类型放在他们自己的命名空间中吗?这是将类分组在一起的更常见方式.
编辑:只是为了澄清,我很少使用公共嵌套类,我可能不会在这里使用它们,但我也不会完全不喜欢它们.框架中有很多公共嵌套类型的例子(例如List<T>.Enumerator) - 毫无疑问,在每种情况下,设计师都会考虑使用嵌套类的"气味",并认为它不是一种气味,而不是将类型推广为顶级的,或为所涉及的类型创建新的命名空间.
| 归档时间: |
|
| 查看次数: |
5683 次 |
| 最近记录: |