Over a million developers have joined DZone.

Strategy Pattern Implemented as an Enum Using Lambdas

Learn how to implement strategy patterns by developing a calculator in Java.

· Java Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

In this post I would like to show how the strategy pattern can be implemented as an enum with lambdas.

The Strategy Pattern is one of the Gang of Four design patterns published in their book: Elements of Reusable Object-Oriented Software. The intent of the strategy pattern is:

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

We will develop a simple calculator where the strategies are basic arithmetic operations. We start with an interface that defines a single abstract method.

public interface OperationStrategy {
    T compute(T x, T y);

We now implement each arithmetic operation using a lambda expresion.

public enum Operation implements OperationStrategy {

    ADD((x, y) -> x + y),
    SUBTRACT((x, y) -> x - y),
    MULTIPLY((x, y) -> x * y),
    DIVIDE((x, y) -> x / y),

    private OperationStrategy operationStrategy;

    Operation(final OperationStrategy operationStrategy) {
        this.operationStrategy = operationStrategy;

    public Double compute(Double x, Double y) {
        return operationStrategy.compute(x, y);

A series of tests prove that it works.

public class OperationStrategyTest {

    public void shouldAddTwoNumbers() {
        assertThat(Operation.ADD.compute(5d, 5d)).isEqualTo(new Double(10));

    public void shouldSubtractTwoNumbers() {
        assertThat(Operation.SUBTRACT.compute(10d, 5d)).isEqualTo(new Double(5d));

    public void shouldMultiplyTwoNumbers() {
        assertThat(Operation.MULTIPLY.compute(5d, 5d)).isEqualTo(new Double(25));

    public void shouldDivideTwoNumbers() {
        assertThat(Operation.DIVIDE.compute(10d, 2d)).isEqualTo(new Double(5d));

    public void shouldDetermineMaximumOfTwoNumbers() {
        assertThat(Operation.MAX.compute(10d, 5d)).isEqualTo(new Double(10d));

The use of lambdas as strategies reduces boilerplate code quite substantially, though it would not be correct to use lambdas if the strategy is complex and requires a lot of code. It would become cumbersome.

A git repository of this code is available here.

Building Identity Management, including authentication and authorization? Try Stormpath! Our REST API and robust Java SDK support can eliminate your security risk and can be implemented in minutes. Sign up, and never build auth again!


Published at DZone with permission of Alex Theedom. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}