Constructors with too many parameters

MJDeveloping :

Without giving too much away, I'm making an object which has 9 attributes. Let's use an RPG for example.

float strength, constitution, fortitude, dexterity, intelligence, charisma, wisdom, willpower, perception;

Given that the object is called player, the constructor of

public Player(float strength, float constitution, float fortitude, float dexterity, 
float intelligence, float charisma, float wisdom, float willpower, float perception){}

According to SonarLint: "Constructor has 9 parameters, which is greater than 7 authorized."

While I know that SonarLint saying that won't stop the code actually functioning, if there is a "proper" way of dealing with these situations, I'd love to know!

Thanks

P.s, if this is the wrong forum for such a question, please point me towards the right one!

Tom Hawtin - tackline :

Ideally you would split the class into smaller units and compose.

In this case, as they seem to be all of the same kind of thing, a Map (specifically EnumMap) would appear to be appropriate.

As a last resort, there is the Builder Pattern.

 Player player = Player.builder()
     .strength(       )
     .constitution(   )
     // ...
     .perception(     );

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=370596&siteId=1