您将如何根据这些要求设计您的 类?

How will you design your classes with this requisites?

下午好; 假设我们有一个名为 element 的对象,其属性如下:

name type description
id integer element identifier
name string element name
origin integer element origin:
0 = natural
1 = artificial
2 = mix
3 = none
4 = gov
class integer element class:
- If origin = 0 then:
0 = rainy
1 = sunny
2 = foggy
3 = cloudy
4 = none
- If origin = 1 OR origin = 4 then:
0 = nonatural
1 = disaster
subclass integer element subclass:
If origin = 0 & class = 4 then:
0 = investigate
1 = talk
2 = rest
If origin = 1 then:
0 = good
1 = bad
2 = none
If source = 4 then:
0 = countryA
1 = countryB
2 = countryC

我想要的是将其转化为 classes 以便能够控制该枚举的“更改值”(?)(从 xml 文件中获取值):

而 class 可能是这样的:

public int id { get; set; }
       
public string name { get; set; }
       
public ElementOrigin origin { get; set; }
     
public ElementClassIfOrigin0 class { get; set; }
     
public ElementClassIfOrigin1Or4 class2 { get; set; }


public enum ElementOrigin 
    {
        
        [XmlEnum("0")] natural,
        [XmlEnum("1")] artificial,
        [XmlEnum("2")] mix,
        [XmlEnum("3")] none,
        [XmlEnum("4")] gov
    }

    
    public enum ElementClassIfOrigin0 
    {
        
        [XmlEnum("0")] rainy,
        [XmlEnum("1")] sunny,
        [XmlEnum("2")] foggy,
        [XmlEnum("3")] cloudy
    }
    
    public enum ElementClassIfOrigin1Or4 
    {
        
        [XmlEnum("0")] nonatural,
        [XmlEnum("1")] disaster
    }

如果开发一个可以处理所有可能场景的逻辑,我想要的是什么,但是由于枚举可以改变很多,就像那样table;在这种情况下准备 classes 的最佳方法是什么(多态性会有帮助?)

因为我也试图避免这样的事情:

case ElementOrigin.Natural
                        when element.classIfOrigin1== ClassIfOrigin0.natural:
                        break;

case ElementOrigin.Natural
                        when element.classIfOrigin1 == ClassIfOrigin0.artificial:
                        break;

case ElementOrigin.Natural
                        when element.classIfOrigin1 == ClassIfOrigin0.none:
                        break;
//and so on..
    

关于如何进行的任何想法? 非常感谢。

如果 classsubclass 属性只是数字而不需要是 enums (正如@rfmodulator 在他们的评论中所建议的那样),您可以尝试这种方法创建 class 时:

public class Element
{
    public Element(int id, string name, Origin origin) => (Id, Name, Origin) = (id, name, origin);

    public int Id { get; set; }
    public string Name { get; set; }
    public Origin Origin { get; set; }
    
    public WeatherType? WeatherType { get; set; }
    public Classification? Classification { get; set; }
    public Reaction? Reaction { get; set; }
    public Rating? Rating { get; set; }
    public Country? Country { get; set; }

    public int? Class => Origin switch
    {
        Origin.Natural => (int?)WeatherType,
        Origin.Artificial => (int?)Classification,
        Origin.Gov => (int?)Classification,
        _ => null
    };

    public int? SubClass => Origin switch
    {
        Origin.Natural when Class == (int)WeatherType.None => (int?)Reaction,
        Origin.Artificial => (int?)Rating,
        Origin.Gov => (int?)Country,
        _ => null
    };
}

public enum Country
{
    CountryA = 0,
    CountryB = 1,
    CountryC = 2
}

public enum Rating
{
    Good = 0,
    Bad = 1,
    None = 2
}

public enum Reaction
{
    Investigate = 0,
    Talk = 1,
    Rest = 2
}

public enum Classification
{
    Nonnatural = 0,
    Disaster = 1
}

public enum WeatherType
{
    Rainy = 0,
    Sunny = 1,
    Foggy = 2,
    Cloudy = 3,
    None = 4
}

public enum Origin
{
    Natural = 0,
    Artificial = 1,
    Mix = 2,
    None = 3,
    Gov = 4
}

属性 WeatherTypeClassificationReactionRatingCountry 可以在 UI 中有条件地显示和切换,这将再次导致 ClassSubClass.

的适当值

如果您还需要在实例化时填充 ClassSubClass,您可以添加另一个构造函数来填充“基本”属性:

public Element(int id, string name, Origin origin, int? classValue = null, int? subClassValue = null)
    : this(id, name, origin)
{
    switch (origin)
    {
        case Origin.Natural:
            WeatherType = (WeatherType?)classValue;
            if (classValue == (int)WeatherType.None)
            {
                Reaction = (Reaction?)subClassValue;
            }
            return;
        case Origin.Artificial:
            Classification = (Classification?)classValue;
            Rating = (Rating?)subClassValue;
            return;
        case Origin.Gov:
            Classification = (Classification?)classValue;
            Country = (Country?)subClassValue;
            return;
    }
}