Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Behavioral Design Patterns: Command

DZone 's Guide to

Behavioral Design Patterns: Command

Want to learn more about implementing behavioral design patterns on your Java projects? Check out this post where we take a look at the command pattern.

· Java Zone ·
Free Resource

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

The command pattern is quite different from the CoR pattern since it helps us delegate an action/request to another object that is 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. This is pretty much carried out the same way that the 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 a bet  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 that contains our bet actions. This is going to be our BetBook.

package com.gkatzioura.design.behavioural.command;

public class BetBook {

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

}


It is time for us to specify the bet command. The bet command is going to be applied to our BetBook.

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));
    }

}


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.

Topics:
design patterns ,java ,tutorial ,command ,code ,backing ,bet ,chain of responsibility

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}