简体   繁体   English

数据库体系结构中的Java类

[英]Java classes from database architecture

I have a relatively simple database containing a Player table (a single field username ) and a Game (a single field date ) one. 我有一个相对简单的数据库,其中包含一个Player表(一个字段username )和一个Game (一个字段date )。 As a Player can take part in multiple Game and a Game can be played by multiple Player , I use a many-to-many relationship where an extra column score is added to the joined table to store the score of a Player in a Game . 由于一个Player可以参加多个Game并且一个Game可以由多个Player玩,所以我使用了many-to-many关系,其中将额外的列score添加到联接表中以在Game存储Player的分数。

In my Java code, I have obviously a Player class with field username and a Game one with field date . 在我的Java代码中,我显然有一个具有字段usernamePlayer类和一个具有字段dateGame类。 However, I do not know if I should create a third class for the join table or if I should add a List<Player> attribute within the Game class along with a list of List<Integer> to store the score. 但是,我不知道是否应该为联接表创建第三个类,还是应该在Game类中添加List<Player>属性以及List<Integer>来存储分数。

EDIT: Example 编辑:示例

Player table: Player表:

player_id  player_name
    1         Bob
    2         Lea
    3         John

Game table: Game桌:

game_id   game_date
1         20/08/2017
2         19/08/2017

Joined table: Joined表:

game_id  player_id  score
1          1        50
1          2        35
2          1        50
2          3        29

I would not create a separate class for the joined table. 我不会为joined表创建一个单独的类。 I would go with a Map<Player, Integer> in the Game class to hold the players in the game and their respective scores. 我将在Game类中使用Map<Player, Integer>来容纳Game的玩家及其各自的得分。

Something like so (simple, no error checks): 像这样(简单,没有错误检查):

class Game {

    Map<Player, Integer> playerScores = new HashMap<>();

    void addPlayer(Player p) {
        playerScores.put(p, 0);
    }

    void incrementScore(Player p, int increment) {
        int newScore = getScore(p) + increment;
        playerScores.put(p, newScore);
    }

    int getScore(Player p) {
        return playerScores.get(p);
    }

    // etc...

}

Why you don't add score attribute to Player class? 为什么不将分数属性添加到Player类?
1 player probably can't be in 2 games at same time, right? 1位玩家可能无法同时参加2场比赛,对吧?

Reason why I say this is because you would then need only one list, with your approach you end up with 2 lists which are not directly connected. 我之所以这样说,是因为您只需要一个列表,而您的方法最终会得到2个未直接连接的列表。 Yes I know that you will place player and his score at same index but this complicates things a lot. 是的,我知道您会将球员和他的得分放在同一个索引上,但这使事情变得非常复杂。

Situation that I can think of just now is showing scoreboard, in other to show which player has highest score you need to sort it, how would you do it with your 2 lists? 我现在可以想到的情况是显示记分板,其他情况是显示您需要对得分最高的球员进行排序,您将如何处理自己的2个列表?

In some complicated way for sure, you would first need to connect first and second list in some structure like map or something, probably you would need to use TreeMap<Integer, List<Players>> , where Integer would be the score and for each score you would need List of players because 2 or more players could have same score. 以某种肯定的复杂方式,您首先需要以某种结构(如map或其他内容)连接第一和第二个列表,可能需要使用TreeMap<Integer, List<Players>> ,其中Integer是得分,每个得分您需要的玩家分数,因为2个或更多玩家可能具有相同的分数。

By having score in your Player class you avoid this complications and make accessing score of each player very easy, once they leave game you simply take score, save in database and set back to 0. 通过在Player类中获得分数,您可以避免这种麻烦,并且可以轻松地访问每个玩家的分数,一旦他们离开游戏,您只需获取分数,保存在数据库中并重新设置为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM