HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Factory for mathematics Q&A

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
factorymathematicsfor

Problem

I'm having a hard time creating a design (due to my lack of experience). I need a little of help to create the Model.

It's a mathematics system. I've created an IProblem interface with a function IsCorrect, this function helps me to know if the result is correct or not (by parameter). It also has a function CreateAnswer() because some problems have 1, 2 or many answers.

public interface IProblem
{
    bool IsCorrect(IAnswer answer);
    IAnswer CreateAnswer();
}


IAnswer is an interface which contains the answer, it can be a string, integer, decimal, several, multiple choice, etc..

public interface IAnswer
{
}

public class IntegerAnswer : IAnswer
{
    public int Number { get; set; }
}

public class StringAnswer : IAnswer
{
    public string Number { get; set; }
}

public class DecimalAnswer : IAnswer
{ ... }

public class MultipleChoiceAnswer : IAnswer
{ ... }


Here is a concrete Problem:

```
public class BinaryProblem : IProblem, IEquatable
{
private int _number1;
private int _number2;

public int Number1
{
get { return _number1; }
set { _number1 = value; }
}

public int Number2
{
get { return _number2; }
set { _number2 = value; }
}

public BinaryProblem(int number1, int number2)
{
this.Number1 = number1;
this.Number2 = number2;
}

public bool IsCorrect(IAnswer answer)
{
IntegerAnswer integerAnswer = (IntegerAnswer)answer;
return integerAnswer.Number == _number1 + _number2;
}

public IAnswer CreateAnswer()
{
return new IntegerAnswer();
}

public override string ToString()
{
return _number1 + " + " + _number2;
}

public override bool Equals(object obj)
{
if (obj is BinaryProblem)
return this.Equals(obj);
else
return false;
}

public bool Equals(BinaryProblem other)
{
return this.Number1 == other.Number1

Solution

Firstly, do you really need it to be that generic? You support arbitrary types and numbers of answers. But unnecessary genericness just make the design overly complicated. If you are only going to every have a single number answer, then just write code to do that.

Secondly, you store the answers on the Problem object. But the answers given are not a property of the problem. It makes more sense to have the problem take in the answers as a parameter to the IsCorrect() function.

Thirdly, your problem interfaces don't appear very useful. It seems that I get access and presumably modify the given answers, and then check if the answers are correct. But what about finding out what the problem is? There's not much point in an interface if you are just going to have to cast down to the actual class to do any work.

Fourthly, your provide getters/setters on the numbers and such for the problem. Do you really need them? Are you really changing the problems after they are created?

Fifthly, an obvious way to use OOP here would be to one class for each operator (multiple, divide, add, subtract, etc).

The way I'd approach this:

interface IProblem
{
    String GetQuestion();
    bool CheckAnswer(int answer);
}

class BinaryOperatorProblem : IProblem
{
     int number1, number2;
     BinaryOperatorProblem(int number1, int number2);
}

class AdditionProblem
{
     String GetQuestion()
     {
           return "%d + %d" % (number1, number2);
     }

     bool CheckAnswer(int answer)
     {
          return answer == number1 + number2;
     }
}


Notes: I don't do C# so above syntax is mostly guesses. I also don't know much about your problem, so you mileage will vary.

Code Snippets

interface IProblem
{
    String GetQuestion();
    bool CheckAnswer(int answer);
}

class BinaryOperatorProblem : IProblem
{
     int number1, number2;
     BinaryOperatorProblem(int number1, int number2);
}

class AdditionProblem
{
     String GetQuestion()
     {
           return "%d + %d" % (number1, number2);
     }

     bool CheckAnswer(int answer)
     {
          return answer == number1 + number2;
     }
}

Context

StackExchange Code Review Q#7784, answer score: 4

Revisions (0)

No revisions yet.