一组学生最初只有一个名字。从第一次考试开始,他们就会有姓名和分数。如何设计这个简单的方法?

发布于 2024-10-04 02:15:26 字数 1750 浏览 0 评论 0原文

我一开始有一组个体

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 种不同的方法来处理这个问题:

  1. 我的程序上将有一个 Start() 方法,然后是一个 Iteration()Start() 采用 Individual[],而 Iteration() 采用 EvaluatedIndividual[]。哈克,但会创造奇迹。如果没有更好的方案,将使用此方法。

  2. EvaluatedIndividual 继承自 Individual。这看起来很干净,但我对此不太确定,因为我希望 Individual 成为一个接口,我的想法是允许我的库/框架的客户端继承它并定义一个根据他的需要设置 Individual 上的方法/字段(Individual 可以有多个值,例如在计算 value 时会考虑这些值) )。如果我遵循这种方法,我必须让他也实现 EvaluatedIndividual 中的类。

  3. 个人是由价值以及评估wasEvaluated构成的。起初 wasEvaluated 为 false,因此任何收集 value 的尝试都会引发异常。我不是特别喜欢这个,但我不知道为什么。

你是如何处理这个问题的?这可能是一个非常常见的模式,但恐怕我不知道任何与此场景匹配的 GoF 模式:(

谢谢

I have a set of Individuals 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:

  1. I will have a Start() method on my program and then an Iteration(). The Start() takes Individual[] while the Iteration() takes EvaluatedIndividual[]. Hacky, but will work wonders. Will use this if nothing better comes up.

  2. EvaluatedIndividual inherits from Individual. This looks clean, but I am not too sure about it because I'd like Individual 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 on Individual as he wants (Individual could have multiple values that'd be taken into consideration for calculating value, for instance) . If I follow this approach, I'd have to make him also implement a class from EvaluatedIndividual.

  3. Individual is formed by value and a both evaluation and wasEvaluated. At first wasEvaluated was false, so any attempt to gather value 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太阳男子 2024-10-11 02:15:26

我完全赞成简单;我只在这里上一节课。在 C# 中,可以使用 Nullable

double? Score {get;set;}

在 Java 中(显然缺少 Nullable),可能只是将分数与 isEvaluated 一起公开> 标志(或评估的考试数量) - 如果这是false(或0),您将忽略分数。

I'm all for simple; I'd just have one class here. In C# maybe use Nullable<T>:

double? Score {get;set;}

In Java (which obviously lacks Nullable<T>), maybe just expose the score along with an isEvaluated flag (or a count of the number of exams evaluated) - if this is false (or 0) you just ignore the score.

千寻… 2024-10-11 02:15:26

我将有一个类具有特殊的 score 值,表示该个体尚未被评估:

class Individual {
    private double value;
    private double score = -1; // score was calculated based on value,
                               // -1 means not evaluated

    public boolean isEvaluated() { return score != -1; }
}

恕我直言,继承在这里是一种矫枉过正。

I would have one class with a special value for score that denotes that the individual hasn't been evaluated:

class Individual {
    private double value;
    private double score = -1; // score was calculated based on value,
                               // -1 means not evaluated

    public boolean isEvaluated() { return score != -1; }
}

IMHO inheritance is an overkill here.

刘备忘录 2024-10-11 02:15:26

嘿,如果这不是某种课堂练习,请不要让事情变得过于复杂。 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.

情丝乱 2024-10-11 02:15:26
class Individual {
  double score;
  ScoreState scoreState;
}

我发现每当我需要一个状态时,我需要 2 个状态,然后我很快就需要 2+n,所以不妨创建一个枚举,这允许像“待审核”和其他此类考虑因素的情况,这在以下情况中可能很方便未来,它可以防止过多的标志,这可能会造成混乱。

class Individual {
  double score;
  ScoreState scoreState;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文