0

Question might be silly for experts but please help me with pointers if it is already solved somewhere.

Interview Question : "Design class diagram in best possible way"

  1. You need to design a game "SuperHeroes".

  2. Super hero examples are Batman, Spider-Man, Thor, Hal Jordan, Wonder Woman, Captain America .... n

  3. Spiderman can jump, crawl, generateFire ....n

  4. Batman can jump, crawl, fly .... n
  5. Thor can swim, fly .... n

  6. There can be millions of behaviour.

  7. There can be millions of Super heroes.

  8. Some have few behaviours common in them and some specific to hero.

  9. Design should be flexible enough to add behaviours to the super heroes

Important point to focus was told that "System should be scalable"

I tried twisting decorator pattern to accommodate problem requirements but was failing at many places, also I have to make many interfaces for this, so scalability was questionable.

I tried another approach as Writing all behaviours in one class(If require will classify behaviours in respective classes, kind of utility class which will have all implementations of behaviours). and an Spiderman class which will have list of allowable Behaviours(kind of enum). and it is allowed to call methods from behaviour utility only if such behaviour is allowed in list. I think it is not a good approach.

Please help me with best way to achieve this.

Jayesh
  • 6,047
  • 13
  • 49
  • 81

2 Answers2

1

If I understood the question correctly, the problem could be solved with the mixin pattern; however, multiple inheritance is required for a straightforward implementation, which is not available in Java. The subject is discussed in this question.

Community
  • 1
  • 1
Codor
  • 17,447
  • 9
  • 29
  • 56
1

In games it is pretty easy to get a very huge inheritance tree up to the point, where it is very difficult, if not impossible to add a new entity with a different behaviour. To solve this, something called the Entity Component System is used. It is very flexible, does not limit you to inheritance and is commonly used in larger games.

There is also a follow-up article that describes a specific implementation, and has examples on how it can be used in different situations.

Mario Dekena
  • 843
  • 6
  • 20