简体   繁体   English

调查平台使用哪种设计模式以及如何使用?

[英]Which design pattern to use and how for survey platform?

I'm trying to solve the best architecture for a survey system and which design patterns to use. 我正在尝试为调查系统解决最佳体系结构,并使用哪种设计模式。 We have the following type of questions: 我们有以下类型的问题:

  • One correct answer is possible - dropdown or radio buttons 一个正确的答案是可能的-下拉菜单或单选按钮
  • Multiple choice - checkboxes 多项选择-复选框
  • Yes/No - radio buttons with Yes/No 是/否-具有是/否的单选按钮
  • Short Text 短文
  • Long Text 长文字
  • Number - eg When was ...? 数字-例如,什么时候...?
  • Correct order - eg order the answers in the correct order 正确的顺序-例如以正确的顺序排列答案

I'd like to have Drawing API and Validating API So I came up with "Bridge Pattern" which is mostly used for drawing. 我想拥有Drawing API和Validating API,所以我想出了“ Bridge Pattern”,它主要用于绘图。 But there's something I'm missing. 但是我缺少一些东西。

What I tried is this: 我试过的是:

public abstract class Question : IDrawable, IValidatable
{
    protected readonly IQuestionFormatter questionFormatter;
    protected readonly IQuestionValidator validator;

    public string Title { get; set; }

    public Question(IQuestionFormatter questionFormatter, IQuestionValidator validator)
    {
        this.questionFormatter = questionFormatter;
        this.validator = validator;
    }

    public abstract void Draw();

    public abstract bool Validate();

}

    public interface IQuestionValidator
{
    bool ValidateQuestion(IEnumerable<string> userInput, 
                          IEnumerable<string> questionAnswers);
}

    public interface IQuestionFormatter
{
    string FormatQuestion(string title, IEnumerable<string> options);
}

Then I create RefinedAbstractions for MultipleChoiceQuestion, OnePossibleAnswerQuestion and ShortAnswer. 然后,我为MultipleChoiceQuestion,OnePossibleAnswerQuestion和ShortAnswer创建RefinedAbstractions。 The problem is that MultipleChoiceQuestion can have many correct Answers and many UserInput . 问题在于MultipleChoiceQuestion可以具有许多正确答案和许多UserInput

public class MultipleChoiceQuestion : Question
{
    public List<string> Options { get; set; } = new List<string>();
    public List<string> Answers { get; set; } = new List<string>();
    public List<string> UserInput { get; set; } = new List<string>();

    public MultipleChoiceQuestion(IQuestionFormatter questionFormatter, IQuestionValidator validator) : base(questionFormatter, validator)
    {
    }

    public override void Draw()
    {
        var result = questionFormatter.FormatQuestion(Title, Options);
        Console.WriteLine(result);
    }

    public override bool Validate()
    {
        return validator.ValidateQuestion(UserInput, Answers);
    }
}

OnePossibleAnswerQuestion has many Answers but One UserInput . OnePossibleAnswerQuestion有许多答案,但只有一个UserInput

 public class OnePossibleAnswerQuestion : Question
{
    public string UserInput { get; set; }
    public string Answer { get; set; }
    public List<string> Options { get; set; } = new List<string>();

    public OnePossibleAnswerQuestion(IQuestionFormatter questionFormatter, IQuestionValidator validator) : base(questionFormatter, validator)
    {
    }

    public override void Draw()
    {
        var result = questionFormatter.FormatQuestion(Title, Options);
        Console.WriteLine(result);
    }

    public override bool Validate()
    {
        return validator.ValidateQuestion(new List<string> { UserInput }, new List<string> { Answer });
    }
}

ShortAnswer has one Answer and one UserInput . ShortAnswer有一个Answer和一个UserInput I'm not sure how to create the design. 我不确定如何创建设计。 public class ShortAnswer : Question { public string UserInput { get; 公共类ShortAnswer:问题{公共字符串UserInput {get; set; 组; } public string Answer { get; } public string答案{get; set; 组; } }

    public ShortAnswer(IQuestionFormatter questionFormatter, IQuestionValidator validator) : base(questionFormatter, validator)
    {
    }

    public override void Draw()
    {
        var result = questionFormatter.FormatQuestion(Title, new List<string> { "" });
        Console.WriteLine(result);
    }

    public override bool Validate()
    {
        return validator.ValidateQuestion(new List<string> { UserInput }, new List<string> { Answer });
    }
}

But when I got to the OnePossibleAnswerQuestion or ShortAnswer the .Validate() .Draw() started smelling. 但是,当我到达OnePossibleAnswerQuestion或ShortAnswer时, .Validate(). Draw()开始散发香味。 Probably I have defined wrong interfaces? 可能我定义了错误的接口?

My goal is to have something like this: 我的目标是拥有这样的东西:

    List<Question> questions = new List<Question>();

    // ... add some questions using Builder or Factory Design Pattern

    // draw the questions
    foreach (var question in questions)
    {
        question.Draw();
    }

    // validate the questions
    foreach (var question in questions)
    {
        question.Validate();
    }

Could you please help me what I miss and how to composite the classes? 您能帮我我想念的内容以及如何组合课程吗? Am I using the wrong design pattern? 我使用的设计模式错误吗?

Thanks in advance! 提前致谢!

1) Make you validator working with Question but not with Inputs and Answers: 1)让验证者使用“问题”而不是“输入和答案”:

this.validator.Validate(this);

2) Make you formatter working with Question but not with Title and Options: 2)让格式化程序使用Question,但不能使用Title和Options:

this.formatter.Format(this);

= Now you can move your Validate() and Draw() method in base class and not implement in each question type. =现在,您可以在基类中移动Validate()和Draw()方法,而不必在每种问题类型中实现。 Also you will be able to serve new question types that have new attributes, for example audio or pictures. 另外,您将能够提供具有新属性的新问题类型,例如音频或图片。

3) To make this useful you need to use question renderer instead of formatter. 3)为了使此功能有用,您需要使用问题渲染器而不是格式化程序。 What if you decide to change input from text console to HTML page? 如果您决定将输入从文本控制台更改为HTML页面怎么办?

4) Also you might add GetInput() method for questions to have something like this: 4)另外,您可以为问题添加GetInput()方法,例如:

List<Question> questions = new List<Question>();

// draw the questions
foreach (var question in questions)
{
    question.Draw();
    question.GetInput();
    question.Validate();
}

Or you can just Draw() questions and call Validate() on button click. 或者,您也可以只画Draw()问题并在单击按钮时调用Validate()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM