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 中正确实现空对象模式?有不止一种方法吗?如果是这样,最简洁优雅的方式是什么?
虽然已经给出的解决方案很好,但如果您的默认值不是全部或全无,您可能不需要有一个显式类来表示空状态。我认为为所有字段提供默认值是空对象模式。
例如,您可以这样编写您的类:
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)
在 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单音以提高效率。
| 归档时间: |
|
| 查看次数: |
3819 次 |
| 最近记录: |