我创造的数学问题的软件。如你所知,数学问题有很多种。如何改进这种设计(模式)?
在我的软件,一些问题是从一个XML文件(库)得到,并且可以由工厂产生anothers(随机数,你懂的)。
例如,如果我创建二进制问题补充,如果我选择了第一个选项,我可以有一个类从哪里得到THRE文件,并选择其中的一些。或者如果我选择第二个,我可以随机生成问题:
x = random.Next(y, z);
y = random.Next(y, z);
return new BinaryProblem(x, y);
就是这样的。
所以,我已经开发了现在这样的设计,我想我已经建造了策略模式。
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);
}
在服务中,我把IService设置为public,因为我需要知道服务是工厂还是存储库。在这种情况下,我会向文件提交一些问题。
我不相信这个设计。我想我是多余的,这不是最好的办法。
你可以给你opinnion或想法,以改善它?
编辑: 我的第一个选项的意思是:
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;
}
如何考虑的具体问题(资料库)的特殊情况下,随机问题?随机问题除了随机数部分还需要描述,对吗? – nhahtdh 2012-07-10 03:49:10
是的,它就像一个配置..我省略了 – 2012-07-10 03:53:59
我的意思是说,你可以通过一个“管道”来解决问题:首先从XML中获取数据,然后生成随机问题。那么你不需要扩展IService接口的2个类。 – nhahtdh 2012-07-10 03:56:41