I have a list of objects deserealized from different JSON objects to Java classes. Most of them have just one responsibility - they holds data deserialized from JSON files, and give all who needs it access to this data.
Some of them also have some logic realized inside, for example i have a list of classes which implements "Action" interface:
public interface Action {
boolean canAct(Player player);
void act(Player player);
}
public class ActionRestoreHealth implements Action {
@SerializedName("health")
private int health;
public int getHealth() {
return health;
}
@Override
public boolean canAct(Player player) {
return player.health < player.maxHealth;
}
@Override
public void act(Player player) {
if(player.health + health < maxHealth)
player.health += this.health;
else
player.health = player.maxHealth;
player.notifyHealthChanged();
}
}
Now i want to extract all this data classes to separate maven module, to give other projects related to main easy way to use all data loading/deserialization logic.
But dependencies on classes like "Player" in this data holder classes doesn't allowing me to do this. Now if i'll extract this data logic in separate maven module - i'll need provide info about Player class to all clients calling this logic.
I think, that when i implemented interfaces like "Action" directly in data holder classes i broke single responsibility pricipe, plus added strong coupling between data holder class and Player, and this is bad practice too.
It was very convenient to execute every action directly from data holder class: i don't need to know anything about concrete action class, simple calling "canAct" and "act" methods, but seems that the way i implemented it is not best choise.
My question is, is there some best practices/design patterns to break this coupling? Could i move Action interface somewhere to remove data holders class with Player class, and still have convenient way to call "canAct" and "act" methods? How to do this best way?
Aucun commentaire:
Enregistrer un commentaire