疑问如何安排我的课 [英] Doubt about how to organize my classes

查看:111
本文介绍了疑问如何安排我的课的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是只从我的代码的例子。 。我在寻找保持我的课,为了和遵循一些规则OOP一个很好的方法。

Here is only an example from my code. I'm looking for a good way to maintain my classes in order and following some OOP rules.

这我的抽象类问题:

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



下面是一个类从问题派生,Arithetic类包含了所有必要的包含一个数学问题,以及如何解决它:

Below is one class which derives from Problem, Arithetic class contains all the necessary that contains in a math problem, and how to resolve it:

public enum Operations
{
    Addition, Subtraction, Multiplication, Division
}

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;
    }
}

public class Addition : Arithmetic
{
    public Addition(decimal addend1, decimal addend2)
        : base()
    {
        Number1 = addend1;
        Number2 = addend2;
        Operation = Operations.Addition;
    }
}

// Subtraction, Multiplication and Divison here

然后我有一个生成一个算术题另一个类,它接收到一个元组,其中包含一些属性,指示条件

Then I have another class which generate an Arithmetic problem, it receives a Tuple where contains some properties that indicates the conditions

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

public class ArithmeticProblemFactory : IProblemFactory<Arithmetic>
{
    private Tuple<Operations, Range, Range> _condition;

    public ArithmeticProblemFactory(Tuple<Operations, Range, Range> condition)
    {
        this._condition = condition;
    }

    public Arithmetic Create()
    {
        Operations operation = _condition.Item1;
        decimal a = _condition.Item2.GetNumber();
        decimal b = _condition.Item3.GetNumber();

        switch (operation)
        {
            case Operations.Addition:
                return new Addition(a, b);
            case Operations.Subtraction:
                return new Subtraction(a, b);
            case Operations.Multiplication:
                return new Multiplication(a, b);
            case Operations.Division:
                return new Division(a, b);
            default:
                throw new Exception("Operator unexpected");
        }
    }
}



事情是...我需要有更多的属性,如结果(算术类只需要1个,相比2号,我们需要两个属性导致),问题编号,时间(秒)来解决这个问题。

The thing is... I need to have more properties, like Result (in Arithmetic class only needs 1, in comparison 2 numbers we need two properties result), problem number, time (seconds) to resolve the problem.

现在的问题是,我不知道我应该把这些属性。一种方法是将加入他们中的一些问题中的类或创建另一个类的类似:

The question is, I don't know where I should put these properties. One way it'll be adding some of them in Problem class, or create another class something like these:

ArithmeticProblem

ArithmeticProblem


  • 问题问题< - 这是算术级

  • Problem Problem <-- Here is Arithmetic class

结果

时间

问题数

我只希望我的组织类作为必须。
先谢谢了。

I only want to organize my classes as must be. Thanks in advance.

推荐答案

您可以有结果,并在你的算术级接受的结果类型为通用的不同类别

You can have different classes for result and in your Arithmetic class accept result type as generic:

public class Arithmetic < TResult> : ...

增加可波纹管:

public class Addition : Arithmetic <decimal>
...



,但如果(参数的个数像结果,时间,.. )是不固定的(动态)你有一本字典,并将它们存储在词典(其类型),并写出具体的行动,并将它们作为字典的值。

but if the number of parameters (like result, time, ...) are not fixed (dynamic) you can have a dictionary and store them in dictionary (their type) and write specific action and set them as value of dictionary.

这篇关于疑问如何安排我的课的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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