一组学生最初只有一个名字。从第一次考试开始,他们就会有姓名和分数。如何设计这个简单的方法?
我一开始有一组个体
:
class Individual {
double characteristics[];
abstract double CalculateValue();
...
}
每个个体都有一组特征。这些特征将由我的库的用户通过继承来实现:
class MyIndividualABC : Individual {
public MyIndividualABC() {
characteristics = new double[2];
characteristics[0] = 123;
characteristics[1] = 456;
}
public override double CalculateValue() {
return characteristics[0] / characteristics[1]; //for example
}
}
然后我将采用它们并给每个特征一个分数(基于它们的值)。我将其称为迭代。
class EvaluatedIndividual {
double value;
double score; //score was calculated based on value
}
从迭代 2 及以后,我将始终拥有 EvaluatedIndividual
类型的对象。但第一次,我必须以 Individual
类型的对象为主导。
我不想以与其他迭代不同的方式对待第一次迭代。我应该如何处理这个问题?
打个比方,有一个班级的学生。在第一次考试之前,您只知道他们的名字,从第一次考试开始,您将同时知道他们的名字和截至该时刻的考试平均分。
我设计了 3 种不同的方法来处理这个问题:
我的程序上将有一个
Start()
方法,然后是一个Iteration()
。Start()
采用Individual[]
,而Iteration()
采用EvaluatedIndividual[]
。哈克,但会创造奇迹。如果没有更好的方案,将使用此方法。EvaluatedIndividual
继承自Individual
。这看起来很干净,但我对此不太确定,因为我希望Individual
成为一个接口,我的想法是允许我的库/框架的客户端继承它并定义一个根据他的需要设置Individual
上的方法/字段(Individual
可以有多个值,例如在计算value
时会考虑这些值) )。如果我遵循这种方法,我必须让他也实现EvaluatedIndividual
中的类。个人是由价值以及
评估
和wasEvaluated
构成的。起初 wasEvaluated 为 false,因此任何收集value
的尝试都会引发异常。我不是特别喜欢这个,但我不知道为什么。
你是如何处理这个问题的?这可能是一个非常常见的模式,但恐怕我不知道任何与此场景匹配的 GoF 模式:(
谢谢
I have a set of Individual
s at first:
class Individual {
double characteristics[];
abstract double CalculateValue();
...
}
Each individual has a set of characteristics. Those characteristics will be implemented by the user of my library through inheritance:
class MyIndividualABC : Individual {
public MyIndividualABC() {
characteristics = new double[2];
characteristics[0] = 123;
characteristics[1] = 456;
}
public override double CalculateValue() {
return characteristics[0] / characteristics[1]; //for example
}
}
I will then take them and give to each one of them a score (based on their value). I will call this doing an iteration.
class EvaluatedIndividual {
double value;
double score; //score was calculated based on value
}
From iteration2 and beyond, I will be always having at hand objects of type EvaluatedIndividual
. But the first time, I will have to lead with objects of type Individual
.
I wouldn't like to have to treat the first iteration in a different way than the other ones. How should I approach this?
An analogy would be to have a class of students. Before the first exam, you just know their names, and from the 1st exam on you will have both their names and the average of the scores of their exams up to that moment.
I have devised 3 different ways to handle this:
I will have a
Start()
method on my program and then anIteration()
. TheStart()
takesIndividual[]
while theIteration()
takesEvaluatedIndividual[]
. Hacky, but will work wonders. Will use this if nothing better comes up.EvaluatedIndividual
inherits fromIndividual
. This looks clean, but I am not too sure about it because I'd likeIndividual
to be an interface and my idea would be to allow the client of my library/framework to inherit from it and define a set of methods/fields onIndividual
as he wants (Individual
could have multiple values that'd be taken into consideration for calculatingvalue
, for instance) . If I follow this approach, I'd have to make him also implement a class fromEvaluatedIndividual
.Individual is formed by value and a both
evaluation
andwasEvaluated
. At first wasEvaluated was false, so any attempt to gathervalue
throws up an Exception. I don't particulary like this, but I am not sure why.
How'd you approach this? This is probably a pretty common pattern, but I am afraid I don't know any GoF pattern that matches this scenario :(
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我完全赞成简单;我只在这里上一节课。在 C# 中,可以使用
Nullable
:在 Java 中(显然缺少
Nullable
),可能只是将分数与isEvaluated
一起公开> 标志(或评估的考试数量) - 如果这是false
(或0
),您将忽略分数。I'm all for simple; I'd just have one class here. In C# maybe use
Nullable<T>
:In Java (which obviously lacks
Nullable<T>
), maybe just expose the score along with anisEvaluated
flag (or a count of the number of exams evaluated) - if this isfalse
(or0
) you just ignore the score.我将有一个类具有特殊的
score
值,表示该个体尚未被评估:恕我直言,继承在这里是一种矫枉过正。
I would have one class with a special value for
score
that denotes that the individual hasn't been evaluated:IMHO inheritance is an overkill here.
嘿,如果这不是某种课堂练习,请不要让事情变得过于复杂。 GoF 模式并不适合您所知道的一切。
Hey, if this isn't some kind of classroom exercise, don't make things over complicated. GoF patterns aren't for everything you know.
我发现每当我需要一个状态时,我需要 2 个状态,然后我很快就需要 2+n,所以不妨创建一个枚举,这允许像“待审核”和其他此类考虑因素的情况,这在以下情况中可能很方便未来,它可以防止过多的标志,这可能会造成混乱。
I've found whenever I need one state, I need 2, and then I shortly need 2+n, so may as well make an enum, this allows for situations like "Pending Review" and other such considerations, which may be handy in the future, and it prevents against an excess of flags, which can be confusing.