How to implement Null Object pattern on data class in Kotlin?

6.2k Views Asked by At

I have a Kotlin data class:

data class PaymentAccount(
    val accountId: Int,
    val accountNumber: String,
    val title: String
)

This is what I'd do in Java:

Create an abstract class:

public abstract class PaymentAccount {

    protected int accountId;
    protected String accountNumber;
    protected String title;

    public PaymentAccount(int accountId,
                          String accountNumber,
                          String title) {
        this.accountId = accountId;
        this.accountNumber = accountNumber;
        this.title = title;
    }
}

Create null object and extend abstract class:

public class NullPaymentAccount extends PaymentAccount {

    public NullPaymentAccount() {
        super(-1,
                "Invalid account number",
                "Invalid title");
    }
}

Create a real object and extend abstract class too:

public class RealPaymentAccount extends PaymentAccount {

    public RealPaymentAccount(int accountId,
                              String accountNumber,
                              String title) {
        super(accountId,
                accountNumber,
                title);
    }
}

How to implement Null Object pattern in Kotlin properly? Is there more than one way? If so, what is the most concise and elegant way?

4

There are 4 best solutions below

1
On BEST ANSWER

In Kotlin you can do the same, just with less lines of code:

interface Account {
    val accountId: Int
    val accountNumber: String
    val title: String
}

object EmptyAccount : Account {
        override val accountId: Int = 1
        override val accountNumber: String = ""
        override val title: String = ""
}

data class PaymentAccount(
        override val accountId: Int,
        override val accountNumber: String,
        override val title: String): Account

Notice that we also make EmptyAccount singletone for efficiency.

0
On

Better use sealed interface or sealed class. For example:

sealed interface Account {

        val accountId: Int
        val accountNumber: String
        val title: String

        object Empty : Account {
            override val accountId: Int = 1
            override val accountNumber: String = ""
            override val title: String = ""
        }

        data class Payment(
            override val accountId: Int,
            override val accountNumber: String,
            override val title: String
        ) : Account
    }

Usage:

    val account = findAccountById(id = 1)

    when (account) {
        Account.Empty -> TODO()
        is Account.Payment -> TODO()
    }
        

Where:

 val list = listOf(
        Account.Payment(
            accountId = 1,
            accountNumber = "1",
            title = "Bob"
        )
    )

 private fun findAccountById(id: Int): Account {
        return list.firstOrNull { it.accountId == id }?:Account.Empty
    }
0
On

Another way could be to define a secondary constructor for this:

data class PaymentAccount(
    val accountId: Int,
    val accountNumber: String,
    val title: String
){
  constructor() : this(-1,
                  "Invalid account number",
                  "Invalid title")
}
1
On

While the solution you've been given already is fine, you might not need to have an explicit class to represent a null state if your default values aren't all or nothing. I would argue that providing defaults for all of your fields is the null object pattern.

For example, you could write your class like this:

data class PaymentAccount(
    val accountId: Int = -1,
    val accountNumber: String = "Invalid Account Number",
    val title: String = "Invalid Title"
)

And when you construct them:

PaymentAccount(1, "Acct2", "Checking") // Actual data
PaymentAccount() // Default values, therefore a "null" object.

The only real problem is when you only specify some of the values, but that might be ok/desirable:

PaymentAccount(1) // accountNumber and title have defaults