如何在 Kotlin 中的数据类上实现空对象模式?

Joh*_*ive 1 design-patterns null-object-pattern kotlin

我有一个 Kotlin 数据类:

data class PaymentAccount(
    val accountId: Int,
    val accountNumber: String,
    val title: String
)
Run Code Online (Sandbox Code Playgroud)

这就是我在 Java 中所做的:

创建一个抽象类:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

创建空对象并扩展抽象类:

public class NullPaymentAccount extends PaymentAccount {

    public NullPaymentAccount() {
        super(-1,
                "Invalid account number",
                "Invalid title");
    }
}
Run Code Online (Sandbox Code Playgroud)

创建一个真实的对象并扩展抽象类:

public class RealPaymentAccount extends PaymentAccount {

    public RealPaymentAccount(int accountId,
                              String accountNumber,
                              String title) {
        super(accountId,
                accountNumber,
                title);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在 Kotlin 中正确实现空对象模式?有不止一种方法吗?如果是这样,最简洁优雅的方式是什么?

Tod*_*odd 6

虽然已经给出的解决方案很好,但如果您的默认值不是全部或全无,您可能不需要有一个显式类来表示空状态。我认为为所有字段提供默认值空对象模式。

例如,您可以这样编写您的类:

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

当你构建它们时:

PaymentAccount(1, "Acct2", "Checking") // Actual data
PaymentAccount() // Default values, therefore a "null" object.
Run Code Online (Sandbox Code Playgroud)

唯一真正的问题是当您只指定某些值时,但这可能是可以/理想的:

PaymentAccount(1) // accountNumber and title have defaults
Run Code Online (Sandbox Code Playgroud)

  • 看起来不错。但还有一个(无关紧要,但是)问题需要提及。如果由于某种原因,在提供此 NullObject 的代码中,我们需要检查 PaymentAccount 是 EmptyAccount 还是只是一个帐户,该怎么办?我看不出有什么优雅的方法可以做到这一点。 (3认同)

Ale*_*hin 5

在 Kotlin 中,你可以做同样的事情,只是用更少的代码行:

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
Run Code Online (Sandbox Code Playgroud)

请注意,我们还制作了EmptyAccount单音以提高效率。