0

In Java, I have an abstract class Player and the two classes Striker and Defender they both extend the abstract class Player. I have an Interface IAttack and an Interface IDefend. In those 2 interfaces there is the prototype of only one method - the method shoot(for the Interface IAttack) and the method tackle(for the interface IDefend). I've implemented the method shoot(resp. tackle) in the class Striker(resp Defender).With this hierarchy I can create Strikes, who can only shoot the ball to a given distance. I can create defenders, who can tackle somebody only with a given intensity.

What I want is to be able to create strikers(resp. defenders) who can do different tasks(and not only shoot to a given distance). For example I'd like to have a striker who can shoot a penalty, a striker who can play with its head etc.

How can I do that? Do I need necessarily to create more classes for the strikers(I mean a class for the strikers who shoot penalties, a class for the striker who play with head). Is there a more elegant way to have strikers who can do multiple tasks?

2
  • Add another attribute for roles then for each Striker object just set the roles that they have...example Striker.isPenalties(). Commented May 22, 2015 at 18:13
  • what does shoot(resp. tackle) mean? Commented May 22, 2015 at 19:12

2 Answers 2

1

Couple of ways to implement it.

1) Let another Stricker class implement both IShootPenalty, and IAttack behavior. You can create individual required interfaces, and let each player object implement appropriate interfaces. Mix and match them as required.

(or)

2) Create boolean flags for each behavior in abstract class, and set the appropriate flags based on required behavior.

Stricker.setShootPenality(true);
Striker.setShooter(true);
Sign up to request clarification or add additional context in comments.

Comments

0

A nice way would be to have all the properties in a different class. Say for example, a class called StrikerProperties.java. You can have different variables here and set them true or false depending upon the other characteristics of the player. This will enable you to use this for different objects, and also different classes, in cases you want to make teams and all later.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.