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();
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 ?
3 Replies
JavaBot
JavaBot5d ago
This post has been reserved for your question.
Hey @flowi! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st4d ago
subclasses can add additional behavior as long as the contract of the superclass is not violated i.e. everything specified in the superclass should still be possible
JavaBot
JavaBot4d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?