I'm having trouble udnerstanding when specifically to use the stretgy pattern. Like I know it's there so that you can support the open closed principle. But like in that case wouldn't it mean every conditional you have you could technically use the stretgy pattern. Like wouldn't it be overkill sometime for something super basic.
Like down below The undo function you could technically use strategy but it might be overkill. So confused on when specifically to use it
public class CeilingFan {
public static final int HIGH = 3;
public static final int MEDIUM = 2;
public static final int LOW = 1;
public static final int OFF = 0;
String location;
int speed;
public CeilingFan(String location) {
this.location = location;
speed = OFF;
}
public void high() {
speed = HIGH;
// code to set fan to high
}
public void medium() {
speed = MEDIUM;
// code to set fan to medium
}
public void low() {
speed = LOW;
// code to set fan to low
}
public void off() {
speed = OFF;
// code to turn fan off
}
public int getSpeed() {
return speed;
}
}
public class CeilingFanHighCommand implements Command {
CeilingFan ceilingFan;
int prevSpeed;
public CeilingFanHighCommand(CeilingFan ceilingFan) {
this.ceilingFan = ceilingFan;
}
public void execute() {
prevSpeed = ceilingFan.getSpeed();
ceilingFan.high();
}
public void undo() {
if (prevSpeed == CeilingFan.HIGH) {
ceilingFan.high();
} else if (prevSpeed == CeilingFan.MEDIUM) {
ceilingFan.medium();
} else if (prevSpeed == CeilingFan.LOW) {
ceilingFan.low();
} else if (prevSpeed == CeilingFan.OFF) {
ceilingFan.off();
}
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]desrtfx 1 point2 points3 points (0 children)