How to encapsulate a property in a base class?

My script is dedicated to the development of mathematical problems. As an IProblem interface, I thought the two main properties that it should contain are QuestionText and Response . QuestionText will always be a string, but a Response can sometimes be a complex object (a custom Fraction struc) or another data type, such as string, decimal, int, etc.

  public interface IProblem { string QuestionText { get; set; } object Response { get; } bool IsComplete(); bool IsCorrect(); } 

As you can see, Response is an object. I guessed that this data type, because all problems by nature have an answer. And since this is an object, I determine only to receive for future errors (problems with casting).

My idea is later, in a particular class, to access this property ( Response ), without having to throw. Check this?

  public abstract class Problem : IProblem { public string QuestionText { get; set;} public object Response { get; protected set; } public virtual bool IsComplete() { return true; } public abstract bool IsCorrect(); } public class BinaryProblem : Problem { public decimal N1 { get; set; } public decimal N2 { get; set; } public decimal Response { get { return (decimal)base.Response; } set { base.Response = value; } } public override bool IsCorrect() { return N1 + N2 == Response; } } 

And here I am testing the value.

  static void Main(string[] args) { BinaryProblem p = new BinaryProblem(); p.N1 = 2; p.N2 = 4; p.Response = 6; IProblem p2 = p; Console.WriteLine(p2.Response); Console.WriteLine(p2.IsComplete().ToString()); } 

It still works, but I want to know if I am doing what I am doing, or good practice. I have seen other people use the new operator for this. Others do not use the word base .

Is this a good way? Could this be the cause of future errors? Please give me feedback on my design.

EDIT: It really is necessary to access the answer in a non-generic interface.

+8
design c # oop uml object-oriented-analysis
source share
1 answer

Maybe you are looking for something like this? Notice, I left some things because they were not important for a partial solution to the problem (for example, QuestionText). I also left the base class, because it turned out to be nothing more than a through and extra layer. It may not be exactly what you are looking for, but I hope it helps you there.

Firstly, everything is used like this:
Edit: Notice how now they can all be considered non-shared IProblem.

 private static void StackOverflowQuestion() { IProblem<int> problem1 = new IntProblem(2, 4); problem1.Response = 6; IProblem<decimal> problem2 = new DecimalProblem(5, 10); problem2.Response = .5M; Console.WriteLine("Problem 1 is correct: {0}", problem1.IsCorrect()); Console.WriteLine("Problem 2 is correct: {0}", problem2.IsCorrect()); List<IProblem> problems = new List<IProblem>(); problems.Add(problem1); problems.Add(problem2); problems.ForEach(problem => Debug.WriteLine(problem.GetResponse())); } 

Edit: here is not a common interface, so many problems can be used in the list and handled the same way:

 public interface IProblem { object GetResponse(); } 

Here's the interface:
Edit: note that this now implements a non-generic interface.

 public interface IProblem<T> : IProblem { T Response { get; set; } bool IsCorrect(); } 

And here are the classes:
Edit: Note the new GetResponse () methods.

 public class IntProblem : IProblem<int> { private int _number1 { get; set; } private int _number2 { get; set; } public int Response { get; set; } public IntProblem(int number1, int number2) { this._number1 = number1; this._number2 = number2; } public bool IsCorrect() { return this._number1 + this._number2 == Response; } public object GetResponse() { return this.Response; } } public class DecimalProblem : IProblem<decimal> { private decimal _number1 { get; set; } private decimal _number2 { get; set; } public decimal Response { get; set; } public DecimalProblem(decimal number1, decimal number2) { this._number1 = number1; this._number2 = number2; } public bool IsCorrect() { return this._number1 / this._number2 == Response; } public object GetResponse() { return this.Response; } } 
+4
source share

All Articles