Skip to main content
 首页 » 编程设计

java之设计问题 : need instances of an enum member as (property, 值)对

2024年02月05日25myhome

这个问题是我之前回答的问题的衍生问题。 Use of Enum when members are the same as another class's public variables .在实现所提供的答案时,我遇到了另一个设计问题。

我正在编写一个程序来模拟进化。我有一个枚举,用于存储蛋白质可以编码的可能属性列表。蛋白质是由一串氨基酸(即“W”、“A”等)产生的。现在我有一个字典,将一个属性(枚举成员)与一个字符链接起来。同样,我对蛋白质类型进行了相同的设置。

public class Protein { 
    Property property; 
    float value; 
    Type type; 
 
    enum Property { 
        SIZE, 
        ID,      
    } 
    enum Type { 
        STRUCTURAL, 
        NEURAL, 
    } 
 
 
    static Dictionary<string, Property> aminoToProperty 
              = new Dictionary<string, Property> { 
         {"F", Property.SIZE}, 
         {"K", Property.ID}, 
    }; 
 
    static Dictionary<string, Type> aminoToType  
         = new Dictionary<string, Property> { 
         {"A", Type.STRUCTURAL}, 
         {"B", Type.NEURAL}, 
    }; 
 
 
} 

在另一个类(class)中,我正在根据一长串字符创建蛋白质,然后将它们组合起来形成结构。几种蛋白质可以加起来为结构创建各种属性和值。

Structure structure = new Structure(); 
Protein protein = new Protein (aminos) // didn't show this method for simplicity 
switch (protein.property) { 
 
case Protein.Property.SIZE: 
        structure.size += protein.value; 
        break; 
 
ONE FOR EVERY PROPERTY... 
 
} 
 
SAME THING FOR EVERY TYPE... 

我的问题:

1) 当我想向混合中添加新属性时,我需要先将条目添加到枚举中,然后再添加到字典中。这让我觉得我违反了某种最佳实践。

2) 我需要在 switch 中为每个属性和类型保留一个 case,这是不可避免的吗?

3) 有什么方法可以保证在两本词典中,我不会不小心使用同一个字母两次?拥有两本词典有意义吗?

4) 也许我完全以错误的方式解决了这个问题。

注意:在较旧的链接问题中,我将值与枚举一起存储。但在这里我需要有多个具有不同值的实例,所以我认为该解决方案不适用。

很抱歉,如果这令人困惑,但我真的坚持如何最好地设计它以使其面向 future 。我正在用 c# 编写,但我认为这个问题适用于大多数 OO 编程语言。

请您参考如下方法:

首先,如果我不熟悉问题领域,请原谅我;我的例子可能反射(reflect)了这一点。

我认为你的直觉很好,因为枚举解决方案似乎违反了 open/closed principle .

另一种在添加新的蛋白质属性和类型时不需要接触多个地方来解决这个问题的方法是使 ProteinType 和 ProteinProperty 抽象基类接受 Structure 并对其进行操作。

然后蛋白质可以聚合这两种类型,接受一个结构并将结构委托(delegate)给 ProteinType 和 ProteinProperty 实例,以便大多数派生类型可以修改结构。

另一件可以自动化的事情是使用反射将氨基查找到 ProteinType 和 ProteinProperty。

这一切看起来都有些矫枉过正,但最终结果是您现在可以自由地单独定义 ProteinType 和 ProteinProperty 类,而不必担心修改代码的其他区域。

这是一个例子:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Reflection; 
 
namespace ConsoleApplication1 
{ 
    class Program 
    { 
        static void Main() 
        { 
            var structure = new Structure(); 
            var protein = new Protein("F", "B", 0.5F); 
            protein.ApplyToStructure(structure); 
        } 
    } 
 
    public class Protein 
    { 
        private readonly float _value; 
        private readonly ProteinProperty _proteinProperty; 
        private readonly ProteinType _proteinType; 
 
        public Protein(string propertyAmino, string typeAmino, float value) 
        { 
            _proteinProperty = ProteinProperty.FromAmino(propertyAmino); 
            _proteinType = ProteinType.FromAmino(typeAmino); 
            _value = value; 
        } 
 
        public void ApplyToStructure(Structure structure) 
        { 
            _proteinProperty.ApplyToStructure(structure, _value); 
            _proteinType.ApplyToStructure(structure, _value); 
        } 
    } 
 
    public class Structure 
    { 
        public float Size { get; set; } 
        public float Id { get; set; } 
    } 
 
    public abstract class ProteinType 
    { 
        private static readonly Dictionary<string, ProteinType> AllProteinTypes = typeof (ProteinType) 
            .GetNestedTypes(BindingFlags.Instance | BindingFlags.NonPublic) 
            .Where(nestedType => typeof (ProteinType).IsAssignableFrom(nestedType)) 
            .Select(nestedType => (ProteinType) Activator.CreateInstance(nestedType)) 
            .ToDictionary(proteinType => proteinType.Amino); 
 
        public static ProteinType FromAmino(string amino) 
        { 
            ProteinType proteinType; 
            if (!AllProteinTypes.TryGetValue(amino, out proteinType)) 
            { 
                throw new ArgumentException("Invalid amino"); 
            } 
 
            return proteinType; 
        } 
 
        public abstract string Amino { get; } 
 
        public abstract void ApplyToStructure(Structure structure, float value); 
 
        private sealed class StructuralProteinType : ProteinType 
        { 
            public override string Amino { get; } = "A"; 
 
            public override void ApplyToStructure(Structure structure, float value) 
            { 
                // do what you need to do to Structure 
            } 
        } 
 
        private sealed class NeuralProteinType : ProteinType 
        { 
            public override string Amino { get; } = "B"; 
 
            public override void ApplyToStructure(Structure structure, float value) 
            { 
                // do what you need to do to Structure 
            } 
        } 
    } 
 
    public abstract class ProteinProperty 
    { 
        private static readonly Dictionary<string, ProteinProperty> AllProteinProperties = typeof(ProteinProperty) 
            .GetNestedTypes(BindingFlags.Instance | BindingFlags.NonPublic) 
            .Where(nestedType => typeof(ProteinProperty).IsAssignableFrom(nestedType)) 
            .Select(nestedType => (ProteinProperty)Activator.CreateInstance(nestedType)) 
            .ToDictionary(proteinProperty => proteinProperty.Amino); 
 
        public static ProteinProperty FromAmino(string amino) 
        { 
            ProteinProperty proteinProperty; 
            if (!AllProteinProperties.TryGetValue(amino, out proteinProperty)) 
            { 
                throw new ArgumentException("Invalid amino"); 
            } 
 
            return proteinProperty; 
        } 
 
        public abstract string Amino { get; } 
 
        public abstract void ApplyToStructure(Structure structure, float value); 
 
        private sealed class SizeProteinProperty : ProteinProperty 
        { 
            public override string Amino { get; } = "F"; 
 
            public override void ApplyToStructure(Structure structure, float value) 
            { 
                structure.Size += value; 
            } 
        } 
 
        private sealed class IdProteinProperty : ProteinProperty 
        { 
            public override string Amino { get; } = "K"; 
 
            public override void ApplyToStructure(Structure structure, float value) 
            { 
                // do what you need to do to Structure 
            } 
        } 
    } 
} 

编辑

在再次阅读您的问题并尝试围绕问题域思考之后,我想知道 ProteinProperty 和 ProteinType 之间是否存在区别...似乎它们只是对结构有影响的氨基字符的表示。您还指出氨基是一串字符。

如果这是真的,我将用一个基类:Amino 替换 ProteinProperty 和 ProtienType。此外,您可以让 Amino 基类的 ApplyToStructure() 方法获取整个 Protein 实例,这样您就可以在派生的 Amino 类中访问它上面的其他内容。

最后,您可以创建一个基于氨基字符串的 Amino 实例集合,并将它们应用于循环中的结构。

这是更新后的示例:

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Reflection; 
 
namespace ConsoleApplication1 
{ 
    class Program 
    { 
        static void Main() 
        { 
            var structure = new Structure(); 
            var protein = new Protein(0.5F, "FKABAKB"); 
            protein.ApplyToStructure(structure); 
        } 
    } 
 
    public class Protein 
    { 
        private readonly ReadOnlyCollection<Amino> _aminos; 
 
        public Protein(float value, string aminos) 
        { 
            Value = value; 
            _aminos = aminos 
                .Select(Amino.FromAminoCharater) 
                .ToList() 
                .AsReadOnly(); 
        } 
 
        public float Value { get; } 
 
        public void ApplyToStructure(Structure structure) 
        { 
            foreach (var amino in _aminos) 
            { 
                amino.ApplyToStructure(structure, this); 
            } 
        } 
    } 
 
    public class Structure 
    { 
        public float Size { get; set; } 
        public float Id { get; set; } 
    } 
 
    public abstract class Amino 
    { 
        private static readonly Dictionary<char, Amino> AllAminos = typeof(Amino) 
            .GetNestedTypes(BindingFlags.Instance | BindingFlags.NonPublic) 
            .Where(nestedType => typeof(Amino).IsAssignableFrom(nestedType)) 
            .Select(nestedType => (Amino)Activator.CreateInstance(nestedType)) 
            .ToDictionary(proteinProperty => proteinProperty.AminoCharacter); 
 
        public static Amino FromAminoCharater(char aminoCharacter) 
        { 
            Amino amino; 
            if (!AllAminos.TryGetValue(aminoCharacter, out amino)) 
            { 
                throw new ArgumentException("Invalid amino"); 
            } 
 
            return amino; 
        } 
 
        public abstract char AminoCharacter { get; } 
 
        public abstract void ApplyToStructure(Structure structure, Protein protein); 
 
        private sealed class SizeProperty : Amino 
        { 
            public override char AminoCharacter { get; } = 'F'; 
 
            public override void ApplyToStructure(Structure structure, Protein protein) 
            { 
                structure.Size += protein.Value; 
            } 
        } 
 
        private sealed class IdProperty : Amino 
        { 
            public override char AminoCharacter { get; } = 'K'; 
 
            public override void ApplyToStructure(Structure structure, Protein protein) 
            { 
                // do what you need to do to Structure 
            } 
        } 
 
        private sealed class StructuralType : Amino 
        { 
            public override char AminoCharacter { get; } = 'A'; 
 
            public override void ApplyToStructure(Structure structure, Protein protein) 
            { 
                // do what you need to do to Structure 
            } 
        } 
 
        private sealed class NeuralType : Amino 
        { 
            public override char AminoCharacter { get; } = 'B'; 
 
            public override void ApplyToStructure(Structure structure, Protein protein) 
            { 
                // do what you need to do to Structure 
            } 
        } 
    } 
} 

另请注意,这解决了意外引入重复项的问题。如果您定义一个 Amino 类,其中一个字母已经被另一个字母占用,您的应用程序将立即崩溃并燃烧,而不是出现细微的错误。