Software Development

Behavioural Design Patterns: Command

Previously we used the Chain of Responsibility pattern in order to handle a complex problem with road incidents in a region, solving the problem in the first place or forwarding it to another incident handler.

The command pattern is quite different to the cor pattern since it helps us in order to delegate an action/request to another object capable of executing it. Thus there is no direct execution whatsoever.

For the command pattern we need an object to encapsulate all the information needed to perform an action or trigger an event sometime later.

One of the scenarios where it might seem very familiar is when it comes to queues and consuming those messages. Pretty much the same way elastic beanstalk workers consume messages from queues.

Each message in a queue contains a command and the code that handles that message has to execute it.
Let’s do this with an example from the betting industry.
We are going to have some bets being backed and sent over to our booking system.

So let’s create the bet class.

package com.gkatzioura.design.behavioural.command;

public class Bet {

    private final String match;
    private final Integer amount;

    public Bet(final String match, final Integer amount) {
        this.match = match;
        this.amount = amount;
    }

    public String getMatch() {
        return match;
    }

    public Integer getAmount() {
        return amount;
    }
}

Now let’s add a class which shall contain our bet actions. This is going to be our bet book.

package com.gkatzioura.design.behavioural.command;

public class BetBook {

    public void addBacking(String match, Integer amount) {
        /**
         * Add the backing to the book
         */
    }

}

Now it is time for us to specify the bet command. The bet command is going to be applied to our Bet book.

package com.gkatzioura.design.behavioural.command;

public interface BetCommand {

    void applyTo(BetBook betBook);

}

This brings us to the Backing command.

package com.gkatzioura.design.behavioural.command;

public class BackingCommand implements BetCommand {

    private final Bet bet;

    public BackingCommand(final Bet bet) {
        this.bet = bet;
    }

    @Override
    public void applyTo(BetBook betBook) {
        betBook.addBacking(bet.getMatch(),bet.getAmount());
    }

}

So let’s put them all together.

package com.gkatzioura.design.behavioural.command;

import java.util.ArrayList;
import java.util.List;

public class Command {

    public static void main(String[] args) {
        List<BetCommand> betCommands = new ArrayList<>();
        betCommands.add(new BackingCommand(new Bet("match1",10)));
        betCommands.add(new BackingCommand(new Bet("match2",11)));

        BetBook betBook = new BetBook();
        betCommands.forEach(bc->bc.applyTo(betBook));
    }

}

So imagine that we just gathered our bet commands and we got them dispatched to a microservice responsible to apply each command to our bet book.

You can find the source code on github.

Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Behavioural Design Patterns: Command

Opinions expressed by Java Code Geeks contributors are their own.

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button