问题
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:
Here there is the casses
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;
}
}
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.
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.
回答1:
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.
来源:https://stackoverflow.com/questions/6988431/how-can-i-do-a-good-design-for-this-model