How can I make a good design for this model?

I am starting to create chart classes. Necessity obliges me to make a good model.

Solution for math quiz. The system should generate every problem and check the answer.

I am going to show my general classes:

There are ticket offices here

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 have classes that 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; } } 

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

For example, in arithmetic only a decimal value is required, in comparison 2 I need to save two values, for others I need to save the result as a fraction class.

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

0
source share
1 answer

If all possible problems have a different set of results, then what functionality do you hope to reorganize into an interface? They do not have common features. It's your problem. Perhaps you can do something simple, for example

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

However, your current architecture is simply unsuitable for the purpose.

+1
source

All Articles