Looking for an appropriate design pattern

Posted by user1066015 on Stack Overflow See other posts from Stack Overflow or by user1066015
Published on 2011-11-25T17:46:55Z Indexed on 2011/11/25 17:50 UTC
Read the original article Hit count: 176

Filed under:
|
|

I have a game that tracks user stats after every match, such as how far they travelled, how many times they attacked, how far they fell, etc, and my current implementations looks somewhat as follows (simplified version):

Class Player{
    int id;

public Player(){
    int id = Math.random()*100000;
    PlayerData.players.put(id,new PlayerData());
}

public void jump(){
    //Logic to make the user jump
    //...

    //call the playerManager
    PlayerManager.jump(this);
}

public void attack(Player target){
   //logic to attack the player
   //...

   //call the player manager
   PlayerManager.attack(this,target);
}

}

Class PlayerData{
    public static HashMap<int, PlayerData> players = new HashMap<int,PlayerData>();
    int id;
    int timesJumped;
    int timesAttacked;

}
public void incrementJumped(){
    timesJumped++;
}
public void incrementAttacked(){
    timesAttacked++;
}

}

Class PlayerManager{

public static void jump(Player player){
    players.get(player.getId()).incrementJumped();
}
public void incrementAttacked(Player player, Player target){
    players.get(player.getId()).incrementAttacked();
}

}

So I have a PlayerData class which holds all of the statistics, and brings it out of the player class because it isn't part of the player logic. Then I have PlayerManager, which would be on the server, and that controls the interactions between players (a lot of the logic that does that is excluded so I could keep this simple). I put the calls to the PlayerData class in the Manager class because sometimes you have to do certain checks between players, for instance if the attack actually hits, then you increment "attackHits".

The main problem (in my opinion, correct me if I'm wrong) is that this is not very extensible. I will have to touch the PlayerData class if I want to keep track of a new stat, by adding methods and fields, and then I have to potentially add more methods to my PlayerManager, so it isn't very modulized.

If there is an improvement to this that you would recommend, I would be very appreciative. Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about design