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?