Search code examples
design-patternskotlinnull-object-pattern

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


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?


Solution

  • 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.