Java Community | Help. Code. Learn.JC|HCL
Java Community | Help. Code. Learn.โ€ข9mo agoโ€ข
5 replies
flowi

Am I violating Open Closed Principle ?

public class ShadowBeast extends Actor { 
    private Map<Integer, Behaviour> activityPriorities = new TreeMap<>();

    public ShadowBeast() {
        super("Shadow Beast", 'b', 75); 
        activityPriorities.put(0, new AmbushBehaviour()); // Add behaviour with priority 0
        activityPriorities.put(1, new ProwlBehaviour());  // Add behaviour with priority 1
    }

    /**
     * Selects and returns an action for the ShadowBeast's turn.
     * Iterates through its behaviours based on priority.
     *
     * @param actions    Collection of possible Actions for this Actor
     * @param map        The map containing the Actor
     * @return The Action to be performed, or DontDoAnythingAction
     */
    @Override
    public Action playTurn(ActionList actions, Action lastAction, GameMap map, Display display) {
        for (Behaviour currentActivity : activityPriorities.values()) { 
            Action action = currentActivity.getAction(this, map);
            if (action != null) {
                return action;
            }
        }
        // No suitable behaviour found
        return new DontDoAnythingAction();


Essentially, is it a violation of open closed principle if I am putting in new behaviours ?
Was this page helpful?