我怎样才能为这个模型做一个好的设计? [英] How can I do a good design for this model?

查看:40
本文介绍了我怎样才能为这个模型做一个好的设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是创建图表类的初学者.必要性迫使我做一个好的模型.

I'm a beginner creating diagram classes. The necessity obligates me to do a good model.

解决方案是数学测验.系统必须生成每个问题并检查答案.

The solution is for a math quiz. The system must generate each problem and check the answer.

我要展示我的一般课程:

I'm going to show my general classes:

这里有案例

interface IProblemFactory<T> where T : Problem<T>
{
    T Create();
}

public class ArithmeticProblemFactory : IProblemFactory<Arithmetic>
{
    // This generates Arithmetic problems
    public Arithmetic Create() { }
}

// And the others types of problems

然后我得到了包含问题的类:

Then I've got the classes which contain the problem:

public abstract class Problem<T> : IEquatable<T>
{
    public abstract int ResultCount { get; }
    public abstract bool CheckTheAnswer();
    protected abstract bool CheckTheAnswer(params object[] results);
    public abstract bool Equals(T other);
}

public class Arithmetic : Problem<Arithmetic>
{
    public decimal Number1 { get; set; }

    public Operations Operation { get; set; }

    public decimal Number2 { get; set; }

    public override int ResultCount
    {
        get { return 1; }
    }

    protected override bool CheckTheAnswer(params object[] results)
    {
        if (results.Length != ResultCount)
            throw new ArgumentException("Only expected " + ResultCount + " arguments.");

        decimal result = (decimal)results[0];

        switch (Operation)
        {
            case Operations.Addition:
                return Number1 + Number2 == result;
            case Operations.Subtraction:
                return Number1 - Number2 == result;
            case Operations.Multiplication:
                return Number1 * Number2 == result;
            case Operations.Division:
                return Number1 / Number2 == result;
            default:
                throw new Exception("Operator unexpected");
        }
    }

    public override bool Equals(Arithmetic other)
    {
        if (other == null)
            return false;

        return this.Number1 == other.Number1 && Number2 == other.Number2;
    }
}

这个问题我觉得我没有做好设计.因为所有的问题都会包含一个 CheckTheAnswer(params obj..).但所有问题都有不同的结果.

The problem I think I'm not doing a good design. Because all the problems will contain a CheckTheAnswer(params obj..). But all problems have different results.

例如在 Arithmetic 中只需要一个十进制值,在比较 2 中我需要存储两个值,其他我需要像 Fraction 类一样存储结果.

For example in Arithmetic is neccesary only a decimal value, in comparison 2 I need to store two values, others I need to store the result like Fraction class.

也许我需要将它们分开.. 算术可能只包含两个属性:问题和答案,但我不确定.

Maybe I need to separate them.. Arithmetic might contain only two properties: a problem and answer but I'm not sure.

推荐答案

如果所有可能的问题都有不同的结果,那么您希望将哪些功能重构为接口?它们没有任何通用功能.这是你的问题.你可能可以做一些简单的事情,比如

If all of the possible Problems have a different set of results, then what functionality are you hoping to refactor out into an interface? They don't have any common functionality. This is your problem. You might be able to do something simple, like

interface Problem {
    void OutputProblem(OutputStream stream);
    void AcceptAnswer(InputStream stream);
    bool IsAnswerCorrect();
}

但是,您当前的架构根本不适合用途.

However, your current architecture is simply unfit for purpose.

这篇关于我怎样才能为这个模型做一个好的设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆