Doubt about how to organize my classes

Here is just an example from my code. I am looking for a good way to keep my classes in order and follow some OOP rules.

This is my abstract class. Problem:

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

The following is one class that arises from the problem, the Arithetic class contains everything that is needed in the mathematical problem, and how to solve 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 that generates an arithmetic problem, it gets a Tuple, which contains some properties that indicate 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"); } } } 

The thing is ... I need to have more properties, for example Result (in the Arithmetic class only 1 is required, to compare 2 numbers we need two results of the properties), problem number, time (seconds) to solve the problem.

The question is, I don’t know where I should put these properties. One way to add some of them to the Problem class or create another class:

Arithmeticproblem

  • Problem Problem <- Here is an arithmetic class

  • Result

  • Time

  • Issue number

I just want to organize my classes, as it should be. Thanks in advance.

+4
source share
2 answers

You can have different classes for the result, and in your Arithmetic class take the result type as general:

 public class Arithmetic < TResult> : ... 

and Addition may be lower:

 public class Addition : Arithmetic <decimal> ... 

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

+1
source

I would suggest placing the result, time (for generation or solution) and problem number inside the problem class. Each problem will have its own result, time and number; they may also be included. This makes it easy for you to associate a different class with each problem, as well as having a comprehensive class of problems.

0
source

All Articles