How to model multiple inheritance objects in Java

I have the following problem in a small soccer manager game that I am writing. I have classes Person, Player, Coach, Manager. Man is the base class of others. The problem is that the player may also be a coach and / or manager. By adding more roles (e.g. apprentice), I am becoming more and more complex - so how can you effectively implement this? Is there no template for this?

+3
source share
4 answers

Do not model the role as a type of person. A person must have a set of roles

public enum Role {
  PLAYER,
  COACH,
  REF 
}

public class Player {
  private final String name;
  private Collection<Role> roles = new ArrayList<Role>();

  public Player(String name) {
    this.name = name;
  }

  public void addRole(Role role) {
    this.roles.add(role);
  }
 }
+9
source

Role, Person . (AbstractRole .)

, .

+1

.

, , "" "" . , "".

, , - , .

Role -s Person.

A :

  • Coach
  • .
0

enum Role {
   Player, Coach, Manager, GroundsKeeper
}

class Person {
   final Set<Role> roles = EnumSet.noneOf(Role.class);
}

, .

0

Source: https://habr.com/ru/post/1781473/


All Articles