【问题标题】:How to design a Questionnaire framework/object model?如何设计问卷框架/对象模型?
【发布时间】:2012-06-26 17:31:43
【问题描述】:

我正在为问卷创建一个框架。

问卷有几个问题。我的情况是寻找一个叫Question的类,它支持你想要的任何答案。

我的意思是,有些问题只需要一个答案,另一些需要两个答案,其他问题需要字符串、整数、双精度或开发人员构建的任何新结构(想象一下开发人员正在创建一个使用 Fraction 结构作为答案的数学问题,例如示例)。

换句话说,我需要支持任何数据类型或数量的答案。

所以我正在考虑创建一个名为 Question 的抽象类,其中将包含 Dictionary 的响应。

public abstract class Question
{
    protected Question(string questionText)
    {
        this.QuestionText = questionText;
        this.Responses = new Dictionary<string, object>();
    }

    public string QuestionText
    {
        get;
        set;
    }

    public IDictionary<string, object> Responses { get; protected set; }
}

例如,如果我创建一个新的Question,这将是演示。

public sealed class Question1 : Question
{
    public Question1(string questionText)
        : base(questionText)
    {
    }

    public int? Response1
    {
        get
        {
            int? value = null;

            if (this.Responses.ContainsKey("Response1"))
                value = this.Responses["Response1"] as int?;

            return value;
        }
        set
        {
            this.Responses["Response1"] = value;
        }
    }
}

你觉得这个想法怎么样?我的第一个疑问:我将响应包含在班级而不是另一个独立班级中是否正确。

【问题讨论】:

    标签: c# oop design-patterns


    【解决方案1】:

    谁来回答这些问题?让我们假设人们。鉴于此,一个人可能会以某种方式登录或识别自己。这让我认为 Person 模型应该包含响应,并引用每个问题。

    // pseudocode 
    class Person 
       int PersonID 
       IList Responses     
    
    class Response
       int ResponseID
       int QuestionID 
       string ResponseValue 
    
    class Question
       int QuestionID
       string QuestionText 
       IList AllowedResponses 
       bool AllowsMultipleResponses
    

    【讨论】:

    • 我喜欢你的道具!给我一些时间来测试它......我想属性 ResponseValue 作为字符串不是一个好主意,也许它会作为对象没问题
    【解决方案2】:

    我会从 jcollum 的模型开始,但我会做一些不同的事情,并在使用对象时添加泛型。

    // pseudocode 
    class Person 
       int PersonID 
       IEnumerable<IQuestionResponse> QuestionResponses
    
    //non generic interface to allow all QuestionRespones 
    //to be stored in one typed collection
    interface IQuestionResponse
    
    class QuestionResponse<TResponse> : IQuestionResponse
       Question<TResponse> Question
       IEnumerable<TResponse> Responses
    
    class Question<TResponse>
       string QuestionText 
       IEnumerable<TResponse> AllowedResponses 
       bool AllowsMultipleResponses
    

    【讨论】:

    • IQuestionResponse 是否包含对象属性?
    • IQuestionResponse 将包含除实际响应之外您需要的任何属性和方法。这些将存储在 QuestionResponse.Responses 中并具有适当的类型。
    【解决方案3】:

    如果您在问题中包含回复,您将违反单一职责原则。换句话说,如果问题类为什么要改变,例如您决定更改响应映射到问题的方式。还缺少一个响应类。此外,使用问题和响应列表创建一个 QuestionResponsesMapping 类。

    回答您在评论中发布的问题(java 中的语法):

    这个 QuestionResponsesMapping 可以有一个键为 Question 和 value 的映射

    List<Response<T extends Object & SomeInterface>>
    

    。确保为 Question 和 Response 类实现 hashCode() 和 equals()。通过将 Response 实现为 Response&lt;T extends Object &amp; SomeInterface&gt;&gt; 来满足您能够为响应存储任何类型的数据类型的要求。此处的 SomeInterface 可以具有跨数据类型通用的方法。

    【讨论】:

    • 你能告诉我更多关于 QuestionResponsesMapping 的信息吗?我同意你的回答,但我该如何开始?
    猜你喜欢
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多