2017-12-28 18 views
2

私は(わかりやすくするために改称)のように「グループ」に分け列挙型を持っている:私は望んでいた、今EnumとClass、ベストプラクティスを比較しますか?私のプログラムで

FlowSteps currentStep = ...; 
if (currentStep == GROUP1_Step1) { //Do something } 

public enum FlowSteps 
{ 
    GROUP1_Step1, 
    GROUP1_Step2, 
    GROUP2_Step1, 
    GROUP1_Step2, 
    ... 
} 

それから私、私の意見は、私のような現在のステップをご確認ください

public class FlowSteps 
{ 
    public enum GROUP1 
    { 
     Step1, 
     Step2 
    } 

    public enum GROUP2 
    { 
     Step1, 
     Step2 
    } 
} 

より明確に、今私は何で私のcurrentStepを比較することはできません:私は本当に好きではない、と私はのようなものをthikingたグループの少しこの種をリファクタリングします

FlowSteps currentStep = ...; 
if (currentStep == FlowSteps.GROUP1.Step1) { //Do something } 

私の列挙型のグループ化のこの種を維持する方法上の任意の提案をし、あまりにも多くの複雑さを増すことなくクラスを比較することができること:INGの?

どのように進めますか?

+0

なぜこれをしなかったのですか? 'FlowSteps.GROUP1 currentStep = ...' – OmG

+0

同様に 'GROUP2'があるためです。 –

+0

グループ間にはどのような関係がありますか?フローは複数のグループにまたがって動作するのですか?それは具体的な提案を提供するために現時点では少し抽象的です - あなたは意味のある名前でもう少し肉付けできますか? –

答えて

3

私は、列挙型のリストは、ここで正しいアプローチではないと思います。私は整数を持つクラスのセットを使用して、固有の数を生成します。

public static class FlowSteps 
{ 
    private const int GroupSize = 100; 

    public static class Group1 
    { 
     private const int GroupId = 1; 

     public const int Step1 = FlowSteps.GroupSize * GroupId + 1; 
     public const int Step2 = FlowSteps.GroupSize * GroupId + 2; 
    } 

    public static class Group2 
    { 
     private const int GroupId = 2; 

     public const int Step1 = FlowSteps.GroupSize * GroupId + 1; 
     public const int Step2 = FlowSteps.GroupSize * GroupId + 2; 
    } 
} 

ご覧のとおり、1グループあたり100歩が利用可能です。それで十分でない場合は、グループサイズを増やしてください。 currentStepは現在intです。

+0

さて、彼は現在列挙型を使用しているので、それらから派生することはできません。私は値が定数であることを好むので、継承はオプションではありません。 –

+0

はい、これは最善の解決策のようです。また、複雑さを増すこともありません。 _FlowSteps currentStep_を_int currentStep_に変更するだけです。 –

0

Patrick Hofmanの回答が対象です。しかし、列挙型を保持する必要がある場合は、静的で読み取り専用のクラスを使用してこれを解決できます。これにより、各グループに含めることができるよく知られたステップの共通セットを保持することができます。

enum Steps { 
    Step1, 
    Step2, 
    Step3 
}; 

static class Group1 { 
    static readonly KeyValuePair<int, string> Step1 = 
     new KeyValuePair<int, string>(
      (int)Types.Group1_Value1, 
      Types.Group1_Value1.ToString()); 

    static readonly KeyValuePair<int, string> Step2 = 
     new KeyValuePair<int, string>(
       (int)Types.Group1_Value1, 
       Types.Group1_Value2.ToString()); 
}  

static class Group2 { 
    public static readonly KeyValuePair<int, string> Step1 = 
     new KeyValuePair<int, string>(
      (int)Steps.Step1, 
      Steps.Step1.ToString()); 

    public static readonly KeyValuePair<int, string> Step2 = 
     new KeyValuePair<int, string>(
      (int)Steps.Step2, 
      "Step Two's Other Sneaky Name"); 

    public static readonly KeyValuePair<int, string> Step3 = 
     new KeyValuePair<int, string>(
      (int)Steps.Step3, 
      Steps.Step3.ToString()); 
} 

上記のGroup2.Step2のように、値の比較を行い、ステップの代替名を置き換えることができます。

Group1.Step1.Value == Group2.Step1.Value 

さらに一歩これを撮影あなたがそれらを列挙またはシリアライズする必要がある場合、あなたはその後、List<NameValuePair<int, string>>のコレクション内の各グループを置くことができます。

+1

ありがとう、これは良い解決策でもあります。 KeyValuePairへのアクセス時間は、O(1)にする必要がありますが、この「シンプルな」目的のためにリソースを少し浪費するかもしれません。ホフマンの解決策は私のスーツのために良く働きます。 (私はGroupSizeとGroupIdも削除し、単純な101,102、... 201,202などを使用しています) –

関連する問題