如何避免300个开关块?

时间:2011-09-29 21:08:41

标签: c# switch-statement maintainability

我正在尝试将300个挑战添加到我的程序中,但只有在CompletionValue.IsChecked = false时才显示它们;

如果您正在创建此程序。你将如何存储挑战? 我正在使用一个开关,但有300个案例是矫枉过正,有更好的方法吗?

有关改进代码的任何建议都非常受欢迎。 我对此有点新鲜。

    Random rand = new Random();
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        AnswerValue.Visibility = Visibility.Collapsed;
        Load();

    }


    private void Load()
    {
        int random = rand.Next(1, 4);
        switch (random)
        {
            case 1:
                Challenge1();
                break;
            case 2:
                Challenge2();
                break;
            case 3:
                Challenge3();
                break;
        }
    }

    private void Challenge1()
    {
        DifficultyValue.Text = "20%";
        CompletionValue.IsChecked = false;
        TitleValue.Text = "Chicken or Egg?";
        QuestionValue.Text = "Can you answer the ancient question: Which came first the chicken of the egg?";
        bmp.UriSource = new Uri("Images/Challenge1.png", UriKind.Relative);
        ImageValue.Source = bmp;
        ImageValue.Visibility = Visibility.Visible;
        ResourceValue.Text = "Resource: Brain Games";
        AnswerValue.Text = "The Egg. According to paleontologists, reptiles and dinosaurs existed long before birds and chickens.  Fossilized eggs dating back on hundred millions years have been uncovered. Thus it can be said that eggs came before chickens.";

    }

    private void Challenge2()
    {
        DifficultyValue.Text = "25%";
        CompletionValue.IsChecked = false;
        TitleValue.Text = "Halving Seven";
        QuestionValue.Text = "Can you prove that seven is half of twelve?";
        bmp.UriSource = new Uri("Images/Challenge2.png", UriKind.Relative);
        ImageValue.Source = bmp;
        ImageValue.Visibility = Visibility.Visible;
        ResourceValue.Text = "Resource: Yahoo Questions";
        AnswerValue.Text = "Roman numeral for 12 - XII \n Cut the roman numeral in half. you will get VII, which is 7.";

    }

    private void Challenge3()
    {
        DifficultyValue.Text = "25%";
        CompletionValue.IsChecked = false;
        TitleValue.Text = "Three-coin flip";
        QuestionValue.Text = "You ask a friend about probability, and he tells you the following: The odds of three tossed coins turning up all heads or all tails is one in two, that is, fifty-fifty. That’s because anytime you toss three coins, at least two must match, either two heads or two tails.  So that means the third coin—which is equally likely to be heads or tails—determines the odds.” Is your friend right? If not, what are the odds of three tossed coins turning up all heads or all tails?";
        bmp.UriSource = new Uri("Images/Challenge3.png", UriKind.Relative);
        ImageValue.Source = bmp;
        ImageValue.Visibility = Visibility.Visible;
        ResourceValue.Text = "Resource: Brain Games";
        AnswerValue.Text = "Answer will be available soon";
    }

4 个答案:

答案 0 :(得分:6)

你的挑战看起来非常相似,对吧?在这种情况下,您希望提取出一个公共数据结构,并将每个挑战表示为一段数据。

通过统一代表您的挑战,您可以根据特定挑战ID的挑战数据设置用户界面。

始终可以将数据移动到XML文件,JSON文件或数据库中,但首先要看看简单的C#解决方案是否适合您:

// Note: This example is simplified for readability

// Here is the common data structure that can represent all challenges
private class Challenge
{
    public string Title { get; set; }
    public string Question { get; set; }
    public string ImagePath { get; set; }
}

// All of the challenges are defined right here, as simple data
private Challenge[] _allChallenges = new Challenge[]
{
    new Challenge
    {
        Title = "Chicken or Egg?",
        Question = "Can you answer the ancient question: Which came first the chicken of the egg?",
        ImagePath = "Images/Challenge1.png",
    },
    new Challenge
    {
        Title = "Halving Seven?",
        Question = "Can you prove that seven is half of twelve?",
        ImagePath = "Images/Challenge1.png",
    },
}

// Choosing challenges is as simple as indexing into the array
private void Load()
{
    int random = rand.Next(1, 4);
    Challenge chosenChallenge = _allChallenges[random];
    LoadChallenge(chosenChallenge);
}

// Setting up the UI for a challenge means extracting information from the data structure
private void LoadChallenge(Challenge chosenChallenge)
{
    TitleValue.Text = chosenChallenge.Title;
    QuestionValue.Text = chosenChallenge.Text;
    bmp.UriSource = new Uri(chosenChallenge.ImagePath, UriKind.Relative);
    ImageValue.Source = bmp;
    ImageValue.Visibility = Visibility.Visible;
}

您可以将此视为declarative programming的一种形式。程序的一个重要部分,即挑战本身,已经从命令式语句(设置UI属性)转换为非常简单的数据声明。

通过进行此转换,您甚至可以检查每个挑战,以确保填写所有部分。然后,您将确保为300个挑战中的每个挑战设置标题,问题,资源,答案等。

答案 1 :(得分:4)

您可以将挑战保存在数据库或文件中。我确实看到你使用随机数并只显示1个挑战。 DB可以是

之类的东西
ChallengeId, DifficultyValue, TitleValue ... 

ChallengeId将是问题编号。因此,根据生成的随机数,您可以选择特定的ChallengeId和相关数据。

答案 2 :(得分:2)

你应该真正研究的是封装和多态代码。通过将您喜欢的属性封装到一个类中,您可以更好地表示“挑战”作为一个整体,并且能够重复使用您必须反复键入的部分(.Text = "...")你未来的编码生活无限好。当然,即使对Challenge实体列表进行编码,如下所示也不会很有趣,您必须在某个时间某处输入该数据。我们只是将此视为编码练习,您可以轻松调整下面的代码,从数据库或序列化文件中填充_challenges

public class Challenge
{
    public int Id {get;set;}
    public int Difficulty {get;set;}
    public bool IsCompleted {get;set;}
    public string Title {get;set;}
    public string Question {get;set;}
    public string Answer {get;set;}
}

public class MainPage
{
    private List<Challenge> _challenges;
    private Random rand = new Random();
    public MainPage()
    {
        _challenges = new List<Challenge> {
            new Challenge {
                    Id = 1,
                    Difficulty = 20,
                    Title = "What came first?",
                    Question =  "The chicken or the egg?",
                    Answer = "The egg." },
            new Challenge {
                    Id = 2,
                    Difficulty = 30,
                    Title = "Make 7 from 12?",
                    Question =  "Can you prove 7 is half of 12?",
                    Answer = "VII" }};
    }

    public void LoadChallenge(Challenge challenge)
    {
        Difficulty.Test = String.Format("%{0}", callenge.Difficulty);
        Completeted.Value = challenge.IsCompleted;
        Title.Test = challenge.Title;
        // etc
    }

    public void StartNewChallenge()
    {
        Challenge nextChallenge = null;
        while(nextChallenge == null)
        {
            var nextId = rand.Next(1,2);
            nextChallenge = _challenges
                .Where(x => x.Id == nextId && !x.IsCompleted)
                .SingleOrDefault(); // default to null if completed == true
        }
        LoadChallenge(nextChallenge);
    }

}

答案 3 :(得分:1)

另一种替代方案可能是某种工厂方法:

<强> MyForm.cs

public class MyForm
{
    Random rand = new Random();
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        AnswerValue.Visibility = Visibility.Collapsed;
        Load();
    }

    private void Load()
    {
        int random = rand.Next(1, 4);
        DisplayChallenge(ChallengeFactory.GetChallenge(random));
    }

    private void DisplayChallenge(ChallengeFactory.Challenge challengeToDisplay)
    {
        DifficultyValue.Text = String.Format("{0}%", challengeToDisplay.Difficulty);
        CompletionValue.IsChecked = challengeToDisplay.IsChecked;
        TitleValue.Text = challengeToDisplay.Title;
        QuestionValue.Text = challengeToDisplay.Question;
        ImageValue.Source = challengeToDisplay.ImageSource;
        ImageValue.Visibility = challengeToDisplay.Visible;
        ResourceValue.Text = challengeToDisplay.ResourceValue;
        AnswerValue.Text = challengeToDisplay.Answer;
    }
}

<强> ChallengeFactory.cs

public static class ChallengeFactory
{
    public class Challenge
    {
        public int Difficulty { get; set; }
        public bool IsChecked { get; set; }
        public string Title { get; set; }
        public string Question { get; set; }
        public Uri ImageSource { get; set; }
        public bool Visible { get; set; }
        public string ResourceValue { get; set; }
        public string Answer { get; set; }

        private Challenge(int difficulty, bool isChecked, string title, string question, Uri imageSource, bool visible, string resourceValue, string answer)
        {
            // assign each of the arguments to the instance properties
        }
    }

    public static Challenge GetChallenge(int challengeNumber)
    {
        switch(challengeNumber)
        {
            case 1:
                return new Challenge(20, false, "Chicken or Egg?", "Can you answer the ancient question: Which came first the chicken of the egg?", new Uri("Images/Challenge1.png", UriKind.Relative), true, "Resource: Brain Games", "The Egg...");
            break;
            case 2:
                // new challenge for challenge #2
            break;
            case 3:
                // new challenge for challenge #3
            break;
        }
    }
}

请注意,我已将Challenge类设置为Factory类中的嵌套类。执行此操作的好处是可以创建挑战private的构造函数(含义)除了工厂方法之外,你不能通过任何东西创建“无效”类型的挑战。这样做的坏处是你必须通过在它前面加上包含类,即ChallengeFactory来进一步限定Challenge类。我认为在这种情况下值得额外的限定符。

最终,如果您计划在代码中定义所有挑战,我认为您不得不创建一个交换机 SOMEWHERE 。正如其他人所说的那样,通过在外部数据源(例如数据库)中定义挑战并使用单个方法来读取,解析和显示,您可以显着减少编写代码(以及切换)所需的代码量。创建一个Challenge实例。