对多部分问题进行建模的最佳方法是什么

发布于 2024-11-19 13:21:14 字数 580 浏览 2 评论 0原文

我正在尝试设计一个子类,其中包含对多部分问题的回答以及一些对它们进行评估/采取行动的逻辑。例如,FoodSurvey 将是 BaseSurvey FoodSurvey 的子类,

BaseSurvey
  Name
  Date
  Submit()

FoodSurvey <- BaseSurvey
  DoYouLikeIcecream
  IfSoWhatFlavour
  WouldYouLikeAFreeSample
  SendSample(flavour)
  ...

FoodSurvey 可能有几十个或更多问题,我需要根据其他答案验证每个答案,并运行一些特定于 FoodSurvey(而不是 CarSurvey)的其他流程,这些流程可能取决于多个答案(例如:SendSample(rockyRoad))。

我曾尝试过在每个调查中使用问题类和问题集合的想法,但这很快就开始看起来像一个调查“引擎”,看起来 1.) 太过分了,2.) 容易出错,3.) 有限我可以用来验证答案的逻辑。

关于设计此类课程是否有任何公认的最佳实践?

如果重要的话,这些类最终将在 ASP.NET 网站或 Web 应用程序中使用。

I am trying to design a subclass that will contain responses to multipart questions and some logic to evaluate/act on them. For example, FoodSurvey would be a subclass of BaseSurvey

BaseSurvey
  Name
  Date
  Submit()

FoodSurvey <- BaseSurvey
  DoYouLikeIcecream
  IfSoWhatFlavour
  WouldYouLikeAFreeSample
  SendSample(flavour)
  ...

FoodSurvey could have several dozen or more questions, and I would need to valdate each answer based on others, as well as run some other processes specific to FoodSurvey (as opposed to CarSurvey) that may depend on multiple answers (ex: SendSample(rockyRoad)).

I have toyed with the idea of a Question class with a Questions collection in each Survey but this quickly started to look like a Survey 'engine' which seemed like it 1.) was overkill, 2.) was error prone and 3.) limited the logic I could use to validate answers.

Are there any accepted best practices regarding designing this type of class?

If it matters, the classes will eventually be used in an ASP.NET website or web application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看春风乍起 2024-11-26 13:21:14

我喜欢你开设“问题”课的方法。更详细地说,该模型可以使用类型/对象模式 - 有点像类型和对象之间的关系。代码可能如下所示:

class Question
{
    public string Text { get; set; }
}

class QuestionAnswer
{
    public Question Question { get; set; }
    public string Answer { get; set; }
}

interface ISurveyValidator
{
    bool Validate(SurveyType type, IEnumerable<QuestionAnswer> answers);
}

class SurveyType
{
    public string Name { get; set; }

    public IList<Question> Questions { get; set; }

    public ISurveyValidator Validator { get; set; }

    public Survey CreateSurvey(IEnumerable<QuestionAnswer> answers)
    {
        if (!this.Validator.Validate(this, answers))
            throw new Exception();
        return new Survey
        {
            Type = this,
            Date = DateTime.Now,
            Answers = answers.ToList()
        };
    }
}

class Survey
{
    public SurveyType Type { get; set; }
    public DateTime Date { get; set; }
    public IList<QuestionAnswer> Answers { get; set; }
}

这将允许您为每种调查类型提供自定义验证。

I like your approach of having a 'Question' class. In more detail, this model could use a type/object patter - sort of like the relationship between a type and an object. The code could look like this:

class Question
{
    public string Text { get; set; }
}

class QuestionAnswer
{
    public Question Question { get; set; }
    public string Answer { get; set; }
}

interface ISurveyValidator
{
    bool Validate(SurveyType type, IEnumerable<QuestionAnswer> answers);
}

class SurveyType
{
    public string Name { get; set; }

    public IList<Question> Questions { get; set; }

    public ISurveyValidator Validator { get; set; }

    public Survey CreateSurvey(IEnumerable<QuestionAnswer> answers)
    {
        if (!this.Validator.Validate(this, answers))
            throw new Exception();
        return new Survey
        {
            Type = this,
            Date = DateTime.Now,
            Answers = answers.ToList()
        };
    }
}

class Survey
{
    public SurveyType Type { get; set; }
    public DateTime Date { get; set; }
    public IList<QuestionAnswer> Answers { get; set; }
}

This would allow you to provide custom validation for each survey type.

陌路终见情 2024-11-26 13:21:14

当你说“过度杀戮”时,我认为你对自己太严厉了。在我看来,无论项目规模如何,从一开始就考虑可扩展性和模块化是很好的设计实践。

我喜欢问题课的想法。您可能有一个 Pattern/RegEx 对象的成员数组(无论它们在 ASP.NET 中被称为什么)和一个接受 String 答案并遍历数组尝试匹配它的方法。您可以包含点值、主题、提示等的最终成员。听起来像是自己类别的完美候选者。
就基于对前一个问题的回答的“链接问题”而言,也许这可以像维护答案列表» next_question 配对一样简单地完成,并为那些“不喜欢冰淇淋”的人提供默认情况。可能是哈希表的一个很好的用例,甚至是新类的对象数组 - NextQuestionPair,其中包括“所选答案”和“下一个问题”的成员

编辑:思考树,根据答案选择分支(或) 。缺乏)

I think you're being too hard on yourself when you say 'overkill'. Thinking about scalability and modularity from the start is good design practice in my books, regardless of project size.

I like the idea of a Question class. You could potentially have a member array of Pattern/RegEx objects (whatever they're called in ASP.NET) and a method that takes a String answer, and walks the array trying to match it. You could include final members for point value, topic, hint, etc. Sounds like a perfect candidate for its own class.
In terms of 'linking questions' based on the response to a previous question, perhaps this could be done as simply as maintaining a list of answers » next_question pairings, with a default case for those that 'don't like ice cream". This might be a good use case for a hash table, or even an array of objects of a new class - NextQuestionPair, which includes members for 'answer selected' and 'next question'.

EDIT: Think trees, choosing branches based on answers (or lack thereof)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文