
我想将值与数量和单位相关联,例如以米为单位的长度.这样我的“观察者”可以适当地缩放它.
我正在寻找一种分层枚举,如下所示:
enum Quantity{ Mass.Kg,Mass.g,Length.m,Length.mm} 但这在C#中不存在.
我不确定表达这个的最佳模式,我想出了以下内容.有没有公认的或更好的方法来做到这一点?
using System;using Moq;namespace ConsoleApplication26{ class Program { static voID Main(string[] args) { //use a Mock to play with the API Mock<ITelemetryPublisherFactory> mockTelemetryPublisherFactory = new Mock<ITelemetryPublisherFactory>(); var telemetryPublisherFactory = mockTelemetryPublisherFactory.Object; //example usages var masstelemetryPublisher = telemetryPublisherFactory.GetChannelSamplePublisher<Double>("My Mass",Mass.Kg); masstelemetryPublisher.PublishChannelSampleAtTimeNow(83.4); var lengthTelemetryPublisher = telemetryPublisherFactory.GetChannelSamplePublisher<Int32>("My Height",Length.μm); lengthTelemetryPublisher.PublishChannelSampleAtTimeNow(1800000); //10 years time.. lengthTelemetryPublisher.PublishChannelSampleAtTimeNow(1800000); masstelemetryPublisher.PublishChannelSampleAtTimeNow(120.1); } } public interface ITelemetryPublisherFactory { ITelemetryPublisher<T> GetChannelSamplePublisher<T>(String channelname,Quantity quantity); } public interface ITelemetryPublisher<T> { voID PublishChannelSampleAtTimeNow(T sampleValue); } public abstract class Quantity {} public class Mass : Quantity { private enum Unit { g,Kg } private Readonly Unit _unit; private Mass(Unit unit) { _unit = unit; } public static Quantity Kg {get { return new Mass(Unit.Kg); }} public static Quantity g { get { return new Mass(Unit.g); } } public overrIDe string ToString() { return String.Format("Mass.{0}",_unit); } } public class Length : Quantity { private enum Unit { m,mm,μm,beardSecond } private Readonly Unit _unit; private Length(Unit unit) { _unit = unit; } public static Quantity m { get { return new Length(Unit.m); } } public static Quantity mm { get { return new Length(Unit.mm); } } public static Quantity μm { get { return new Length(Unit.μm); } } public static Quantity beardSecond { get { return new Length(Unit.beardSecond); } } public overrIDe string ToString() { return String.Format("Length.{0}",_unit); } } }解决方法 我认为最好为度量单位创建一个Unit类,并将一个度量单位与一个量相关联的Quantity类.看看 Quantity pattern的想法.由于您还想记录度量单位的“类型”,因此可以创建一个记录该信息的UnitType类: public sealed partial class UnitType { public string name { get; private set; } public UnitType(string name) { name = name; }}public sealed partial class Unit { public string name { get; private set; } public UnitType Type { get; private set; } public Unit(string name,UnitType type) { name = name; Type = type; }} (您应该通过重写Equals和GetHashCode使它们成为正确的值类型)
Unit类可以扩展为例如转换,复合单元,格式化和解析.
然后,您可以在类中定义常见情况:
public partial class UnitType { public static Readonly UnitType Mass = new UnitType("Mass"); public static Readonly UnitType Length = new UnitType("Length");}public partial class Unit { public static Readonly Unit Grams = new Unit("g",UnitType.Mass); public static Readonly Unit Kilos = new Unit("kg",UnitType.Mass); // ...} 或者使用静态类定义“层次结构”:
public static class Mass { public static Readonly UnitType Type = new UnitType("Mass"); public static Readonly Unit Grams = new Unit("g",Type); public static Readonly Unit Kilos = new Unit("kg",Type); ...}public static class Length ... Quantity类也是一个不可变的值类型(只显示其用法):
var eniacWeight = new Quantity(27,Mass.Tons);
或者您可以使用扩展方法来创建数量:
var eniacWeight = 27.Tons();
(自ENIAC起)
总结以上是内存溢出为你收集整理的c# – 我应该使用什么模式来表达层次枚举?全部内容,希望文章能够帮你解决c# – 我应该使用什么模式来表达层次枚举?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)