Tuesday, May 18, 2021

Command pattern java

The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters. Let's create an interface that will handle all of our receivers **ElectronicDevices**. We will add all the command ti proform.
public interface ElectronicDevice {
    public void on();
    public void off();
    public void volumeUp();
    public void volumenDown();
}
Let's make a reciever for Television. It will be extened by ElectronicDevice. Since it is extened by ElectronicDevice we need to add the required methods. We add a varable that hold the value of our volume. Let's also place some functionality in each of those methods.
public class Television implements ElectronicDevice {
    private int volume = 0;
    
    public void on() {
        System.out.println("TV is on");
    }

    public void off() {
        System.out.println("TV is off");
    }
    
    @Override
    public void volumeUp() {
        volume++;
        System.out.println("TV Volume is at: " + volume);
    }
    
    @Override
    public void volumenDown() {
        volume--;
        System.out.println("TV Volume is at: " + volume);
    }
}
We need to create an interface for our all commands to use called Command.
public interface Command {
    public void execute();
    // Added
    public void undo();
}
Next we will create the command to execute. Let's make a class that will turn on the TV called TurnTVOn.
public class TurnTVOn implements Command {
    ElectronicDevice theDevice;
    
    public TurnTVOn(ElectronicDevice newDevice){
        theDevice = newDevice;
    }
    
    @Override
    public void execute() {
        theDevice.on();
    }
    
    @Override // Added
    public void undo() {
        theDevice.off();
    }
}
We will also create a class to turn the TV off called TurnTVOff and of course it turns the TV off.
public class TurnTVOff implements Command {
    ElectronicDevice theDevice;
    
    public TurnTVOff(ElectronicDevice newDevice){
        theDevice = newDevice;
    }
    
    @Override
    public void execute() {
        theDevice.off();
    }

    // Used if you want to allow for undo
    // Do the opposite of execute()
    public void undo() {
        theDevice.on();
    }

}
Let's create a couple of specific commands that we can run through our interface.
public class TurnTVUp implements Command {
    ElectronicDevice theDevice;
    
    public TurnTVUp(ElectronicDevice newDevice){
        theDevice = newDevice;
    }
    
    @Override
    public void execute() {
        theDevice.volumeUp();
    }
    
    @Override
    public void undo() {
        theDevice.volumenDown();
    }
}
This is known as the invoker. It has a method press() that when executed causes the execute method to be called The execute method for the Command interface then calls the method assigned in the class that implements the Command interface.
public class DeviceButton{
    Command theCommand;
    
    public DeviceButton(Command newCommand){
        theCommand = newCommand;
    }
    
    public void press(){
        theCommand.execute();
    }
    // Now the remote can undo past commands
    public void pressUndo(){
        theCommand.undo();
    }
}
The purpose of TVRemote will be to return the type eletronic divice you are using.
public class TVRemote {
    public static ElectronicDevice getDevice(){
        return new Television();
    }
}
Gets the ElectronicDevice to use. TurnTVOn contains the command to turn on the tv When execute() is called on this command object it will execute the method on() in Television
import java.util.ArrayList;
import java.util.List;

public class PlayWithRemote {
    public static void main(String[] args){
        ElectronicDevice newDevice = TVRemote.getDevice();
        TurnTVOn onCommand = new TurnTVOn(newDevice);
Calling the execute() causes on() to execute in Television When press() is called theCommand.execute(); executes Calling the execute() causes off() to execute in Television Now when execute() is called volumeUp() of Television executes. Calling the execute() causes volumeUp() to execute in Television. When press() is called theCommand.execute(); executes. Creating a TV and Radio to turn off with 1 press.
        DeviceButton onPressed = new DeviceButton(onCommand);
        onPressed.press();
Now when execute() is called off() of Television executes
        
        TurnTVOff offCommand = new TurnTVOff(newDevice);
        onPressed = new DeviceButton(offCommand);
        onPressed.press();
Let work with **TurnTVUp**.
        TurnTVUp volUpCommand = new TurnTVUp(newDevice);
        onPressed = new DeviceButton(volUpCommand);
        onPressed.press();
        onPressed.press();
        onPressed.press();
        Television theTV = new Television();
Let's also work with the radio
        Radio theRadio = new Radio();

        // Add the Electronic Devices to a List
        List<ElectronicDevice> allDevices = new ArrayList<ElectronicDevice>();

        allDevices.add(theTV);
        allDevices.add(theRadio);
Send the List of Electronic Devices to TurnItAllOff where a call to run execute() on this function will call off() for each device in the list This calls for execute() to run which calls for off() to run for every ElectronicDevice
        TurnItAllOff turnOffDevices = new TurnItAllOff(allDevices);
        DeviceButton turnThemOff = new DeviceButton(turnOffDevices);
        turnThemOff.press();
It is common to be able to undo a command in a command pattern. To do so, DeviceButton will have a method called undo Undo() will perform the opposite action that the normal Command performs. undo() needs to be added to every class with an execute()
        turnThemOff.pressUndo();
    }
}
To undo more than one command add them to a LinkedList using addFirst(). Then execute undo on each item until there are none left. (This is your Homework)
import java.util.List;
public class TurnItAllOff implements Command {
    List<ElectronicDevice> theDevices;
    
    public TurnItAllOff(List<ElectronicDevice> newDevices) {
        theDevices = newDevices;
    }
    
    @Override
    public void execute() {
        for (ElectronicDevice device : theDevices) {
            device.off();
        }
    }
    
    @Override
    public void undo() {
        for (ElectronicDevice device : theDevices) {
            device.on();
        }
    }
}
Let's compile our program and run, we should get:
TV is on
TV is off
TV Volume is at: 1
TV Volume is at: 2
TV Volume is at: 3
TV is off
Radio is off
TV is on
Radio is on
The Ray Code is AWESOME!!!
Find Ray on:

wikipedia
facebook
youtube
The Ray Code
Ray Andrade

The Strategy Design Pattern a Behavioral Pattern using C++

The Strategy Design Pattern is a behavioral design pattern that enables selecting an algorithm's implementation at runtime. Instead of i...