I'm creating a software for math problems. As you know, there are many types of math problems.
In my software, some problems are gotten from an Xml file (repository) and anothers can be generated by a factory (random numbers, you know).
For example, if I'm creating binary problems as additions, if I choose the first option I can have a class where get thre file and choose some of them. Or if I choose the second one, I can generate the problems as random:
x = random.Next(y, z);
y = random.Next(y, z);
return new BinaryProblem(x, y);
Something like that.
So I've developed right now this design, I guess I've builded a strategy pattern.
public interface IProblemService
{
IEnumerable<Problem> GetProblems();
}
public class ProblemService : IProblemService
{
private readonly IService service;
public ProblemService(IService service)
{
this.service = service;
}
public IService Service
{
get { return service; }
}
public IEnumerable<Problem> GetProblems()
{
return this.service.GetProblems();
}
}
/* =====================================================*/
CONCRETE CLASSES
public interface IService
{
IEnumerable<Problem> GetProblems();
}
// When I want to generate random problems
public abstract class FactoryService : IService
{
public IEnumerable<Problem> GetProblems();
public abstract Generate();
}
// When I want to get problems through a XML file
public class RepositoryService : IService
{
public abstract IEnumerable<Problem> GetProblems();
void Submit(IEnumerable<Problem> problems);
}
In the service I put IService as public because, I need to know if the service is a factory or repository. In the case this will be a repository, I'd submit some problems to the file.
I'm not convinced of the design. I guess I'm being redundant and this is not the best way to do it.
Can you give your opinnion or ideas to improve it?
EDIT: What I meant with the first option is:
public IEnumerable<Problem> GetProblems()
{
if (model == null)
{
model = new List<Problem>();
// Dummy Data.
model.Add(new SimplifyProblem() { Id = "1", Expression = "8 ÷ 2 x 5 ÷ 10", Result1 = 2 });
model.Add(new SimplifyProblem() { Id = "2", Expression = "20 ÷ 2 x 5 - 2", Result1 = 48 });
model.Add(new SimplifyProblem() { Id = "3", Expression = "15 ÷ 5 + 3", Result1 = 6 });
model.Add(new SimplifyProblem() { Id = "4", Expression = "6 + 4² ÷ 8 - 2", Result1 = 6 });
model.Add(new SimplifyProblem() { Id = "5", Expression = "8 + 2 x 4", Result1 = 40 });
model.Add(new SimplifyProblem() { Id = "6", Expression = "8 + 4 x (5 - 3)", Result1 = 16 });
model.Add(new SimplifyProblem() { Id = "7", Expression = "8 - 3 + 5", Result1 = 10 });
// ...
}
return model;
}
I'm not sure why you have two separate interfaces, IProblemService
and IService
; to me, it seems like they do the same thing.
I would separate the generation of random problems (the "factory" part) from the class that actually returns the problems (the "repository" part):
public interface IProblemRepository {
IEnumerable<Problem> LoadProblems();
}
public class XmlProblemRepository : IProblemRepository {
...
}
public class InMemoryProblemRepository : IProblemRepository {
private readonly IEnumerable<Problem> problems;
public InMemoryProblemRepository(IEnumerable<Problem> problems) {
this.problems = problems;
}
public IEnumerable<Problem> LoadProblems() {
return problems;
}
}
public class RandomProblemFactory {
public IEnumerable<Problem> GenerateProblems(int count) {
...
}
}
Then you can either load from an XML file:
repository = new XmlProblemRepository("problems.xml");
or you can generate problems using the factory and source them from an in-memory repository:
factory = new RandomProblemFactory();
problems = factory.GenerateProblems(10);
repository = new InMemoryProblemRepository(problems);