HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Command pattern for non related recievers in Java with command execute and undo operations

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
undooperationsnonwithrelatedjavaforrecieversandcommand

Problem

Below is the code I have written to enhance my understanding of Command Pattern in Java.
As per my study and understanding, when we have some entities [ which we call as receivers ] then Command pattern is useful in encapsulating those unrelated receivers and the operations in command objects. Please provide me feedback on my below implementation of command pattern [more on pattern usage perspective].

  1. AirConditioner.java



package learn.java.commandpattern.recievers;

/**
 * @author krishna.k
 *
 */
public class AirConditioner {
    public enum Mode {
            HEAT, COOL
    }

    private Mode acMode;
    private int temperature = 16;

    /**
     * @param newTemp
     * @return
     */
    public boolean setTempreature(int newTemp) {
            boolean isSuccess = false;
            if (newTemp = 16) {
                    temperature = newTemp;
                    System.out.println("Temperature has been successfully set as " + temperature);
                    isSuccess = true;
            }
            return isSuccess;
    }

    /**
     * @param newACMode
     */
    public void setACMode(Mode newACMode) {
            this.acMode = newACMode;
            System.out.println("AC mode has been set as " + acMode);
    }

    public void switchOnAC() {
            System.out.println("AC has been switched ON");
    }

    public void switchOffAC() {
            System.out.println("AC has been switched OFF");
    }

    public int getTemp() {
            return temperature;
    }

    public Mode getACMode() {
            return acMode;
    }
}


  1. Bulb.java



package learn.java.commandpattern.recievers;

/**
 * @author krishna.k
 *
 */
public class Bulb {
    public void setON() {
            System.out.println("Bulb has been switched ON");
    }

    public void setOFF() {
            System.out.println("Bulb has been switched OFF");
    }
}


  1. Fan.java



```
package learn.java.commandpattern.recievers;

/**
* @author krishna.k
*
*/
pub

Solution

Code smell using instanceof

if (reciever instanceof Fan) {
                ((Fan) reciever).switchOn();
        } else if (reciever instanceof AirConditioner) {
                ((AirConditioner) reciever).switchOnAC();
        } else if (reciever instanceof Bulb) {
                ((Bulb) reciever).setON();
        } else if (reciever instanceof Microwave) {
                ((Microwave) reciever).switchOnMW();
        }


This usage of instanceof is a code smell, what happens when you buy a new fridge, then this class need to learn how to turn off and on the fridge to.

A better approach to this system would making a LinePoweredDevice class/interface, that contains these methods:

public interface LinePoweredDevice {
    public void turnOn();

    public void turnOff();
}


And then implement it in every class, and adjust your on/off command to this new class.

Order of cases in a switch should be easy for humans to read

switch (speed) {
        case FIVE:
                fanSpeed = Speed.FIVE;
                break;
        case FOUR:
                fanSpeed = Speed.FOUR;
                break;
        case ONE:
                fanSpeed = Speed.ONE;
                break;
        case THREE:
                fanSpeed = Speed.THREE;
                break;
        case TWO:
                fanSpeed = Speed.TWO;
                break;
        case ZERO:
                fanSpeed = Speed.ZERO;
                break;
        }


You have the options in the following order:

  • FIVE



  • FOUR



  • ONE



  • THREE



  • TWO



  • ZERO



When looking at this code, it takes extra long to realize all the constants are there, just because they are in a non-human order.

Inconsistent naming for name of the temperature variable

public boolean setTempreature(int newTemp) {
public int getTemp() {


Either have getTemparature() and setTemparature() or getTemp() and setTemp().

OnOffCommand does not respect already on state of a device

This class does not check if a device is already turned on or off, it may happen that this class turns off a light bulb when undoing, even when this bulb was on before the operation even started.

Code Snippets

if (reciever instanceof Fan) {
                ((Fan) reciever).switchOn();
        } else if (reciever instanceof AirConditioner) {
                ((AirConditioner) reciever).switchOnAC();
        } else if (reciever instanceof Bulb) {
                ((Bulb) reciever).setON();
        } else if (reciever instanceof Microwave) {
                ((Microwave) reciever).switchOnMW();
        }
public interface LinePoweredDevice {
    public void turnOn();

    public void turnOff();
}
switch (speed) {
        case FIVE:
                fanSpeed = Speed.FIVE;
                break;
        case FOUR:
                fanSpeed = Speed.FOUR;
                break;
        case ONE:
                fanSpeed = Speed.ONE;
                break;
        case THREE:
                fanSpeed = Speed.THREE;
                break;
        case TWO:
                fanSpeed = Speed.TWO;
                break;
        case ZERO:
                fanSpeed = Speed.ZERO;
                break;
        }
public boolean setTempreature(int newTemp) {
public int getTemp() {

Context

StackExchange Code Review Q#120029, answer score: 2

Revisions (0)

No revisions yet.