1. Overview

In this tutorial, we’ll discuss how to implement stateless objects in Java. Stateless objects are instances of classes with no instance fields.

In Java, all our code must go in a class. When writing an algorithm, we may only need to provide static methods within a class to achieve this.

However, there may be times when we wish to bind our algorithm to a stateless object.

2. Objects Overview Regarding State

When we think about objects in Java, we usually think about objects that contain state in fields, and methods which operate on that state to provide behaviour.

On top of that, we may make objects with unmodifiable fields. These have their state defined at creation time, and are then immutable as their state doesn’t change. In concurrent operations, immutable objects benefit from the same advantages as stateless objects.

Finally, we have objects that have either no fields at all or only compile-time constant fields. These objects are stateless.

Let’s look at why we may wish to use a stateless object.

3. Using a Stateless Object

For our example, let’s take a sorting algorithm that has no state. Let’s choose the Bubblesort as our implementation:

public void sort(int[] array) {
    int n = array.length;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j  array[j + 1]) {
                int swap = array[j];
                array[j] = array[j + 1];
                array[j + 1] = swap;
            }
        }
    }
}

3.1. Multiple Stateless Sorting Implementations

We now want to add the possibility to sort using other sorting algorithms, so we consider the Quicksort algorithm, which is also stateless:

public void sort(int[] array) {
    quickSort(array, 0, array.length - 1);
}
    
private void quickSort(int[] array, int begin, int end) {
    if (begin < end) {
        int pi = partition(array, begin, end);
        quickSort(array, begin, pi - 1);
        quickSort(array, pi + 1, end);
    }
}    

private int partition(int[] array, int low, int high) {
    int pivot = array[high];
    int i = low - 1;
    for (int j = low; j < high; j++) {
        if (array[j] < pivot) {
            i++;
            int swap = array[i];
            array[i] = array[j];
            array[j] = swap;
        }
    }
    int swap = array[i + 1];
    array[i + 1] = array[high];
    array[high] = swap;
    return i + 1;
}

3.2. Choosing Between Implementations

Let’s say that the decision on which algorithm to use is made at runtime.

We need a way to select the right sorting algorithm at runtime. For this, there’s a design pattern we use called the Strategy design pattern.

To implement the Strategy pattern in our case, we’d create an interface called SortingStrategy that contains the signature of the sort() method:

public interface SortingStrategy {   
    public void sort(int[] array);
}

We can now implement each of our sorting strategies as stateless objects that implement this interface. This allows us to switch to whichever implementation we like, while our consuming code uses whichever sorting object is passed to it:

public class BubbleSort implements SortingStrategy {

    @Override
    public void sort(int[] array) {
        // Bubblesort implementation
    }
}

public class QuickSort implements SortingStrategy {

    @Override
    public void sort(int[] array) {
        // Quicksort implementation
    }
    // Other helpful methods
}

Here our classes contain no fields and therefore have no state. However, since there is an object, it can satisfy the common interface we’ve defined for all sorting algorithms – SortingStrategy.

3.3. Singleton Stateless Implementations

We want to introduce a way to provide the user with the chosen sorting strategy. Since the classes are stateless, we don’t need more than one instance from any of them. Therefore, we can do this using the Singleton design pattern.

We can achieve this pattern for instances of a strategy, by using Java enum.

Let’s switch from the class type to enum and add just one constant – INSTANCE. This constant is effectively a single stateless instance of that particular sorting algorithm. As enum can implement a Java interface, this is a neat way to provide a singleton of a strategy object:

public enum BubbleSort implements SortingStrategy {
    
    INSTANCE;

    @Override
    public void sort(int[] array) {
        // Bubblesort implementation
    }
}

public enum QuickSort implements SortingStrategy {
    
    INSTANCE;

    @Override
    public void sort(int[] array) {
        // Quicksort implementation
    }
    // Other helpful methods
}

3.4. Testing the Sorting Strategies

Finally, we write tests to make sure both sorting strategies work and can be easily maintained:

@Test
void givenArray_whenBubbleSorting_thenSorted() {
    int[] arrayToSort = {17, 6, 11, 41, 5, 3, 4, -9};
    int[] sortedArray = {-9, 3, 4, 5, 6, 11, 17, 41};
        
    SortingStrategy sortingStrategy = BubbleSort.INSTANCE;
    sortingStrategy.sort(arrayToSort);
    assertArrayEquals(sortedArray, arrayToSort);
}
    
@Test
void givenArray_whenQuickSortSorting_thenSorted() {
    int[] arrayToSort = {17, 6, 11, 41, 5, 3, 4, -9};
    int[] sortedArray = {-9, 3, 4, 5, 6, 11, 17, 41};
        
    SortingStrategy sortingStrategy = QuickSort.INSTANCE;
    sortingStrategy.sort(arrayToSort);
    assertArrayEquals(sortedArray, arrayToSort);
}

4. Conclusion

In this article, we explored stateless objects in the Java language.

We saw that stateless objects are useful for keeping algorithms that don’t require state and we also looked at how to implement the strategy pattern.

As usual, the code snippets can be found over on GitHub.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.