广泛使用Factory构造函数是个好主意吗



我的问题是如何在处理相对复杂的不可变对象时正确使用Factory构造函数。假设我想返回一个类的实例,其中一些属性已更改。

示例

@immutable
class SomeObject {
final int id;
final String name;
final int upvote;
final int downvote;
final int favorite;
SomeObject({this.id, this.name, this.upvote, this.downvote, this.favorite});
factory SomeObject.upvoted() {
return SomeObject(
upvote: this.upvote + 1 // apparently can't use this keyword here, wrong syntax
);
}
SomeObject.upvoted(SomeObject ref) {
id = ref.id;
// cant change an immutable type
}
SomeObject upvoted() {
return SomeObject(
upvote: upvote + 1,
//... other properties are null, bad idea?
);
}
SomeObject upvotedWithDefaultConstructorUsingReference(SomeObject ref) {
// works but seems like an overkill, especially considering more number of properties
return SomeObject(
id: ref.id,
name: ref.name,
upvote: upvote + 1,
downvote: ref.downvote,
favorite: ref.downvote
);
}
}

SomeObject.upvoted((将是同一类的实例,但其upvoted属性比引用的属性多+1。还有更多类似于downwoted((、withNameChanged((或copyWith((的功能。

前两个是构造函数,其他的只是返回SomeObject类实例的方法。这里应该采取什么方法?当类是不可变的时,我如何使用工厂构造函数?此外,我也不确定这4个例子的区别。

我已经阅读了这个问题的答案,但它似乎没有回答我的问题。

看起来您想要copyWith类型的模式:

class SomeObject {
final int id;
final String name;
final int upVote;
final int downVote;
final int favorite;
SomeObject({this.id, this.name, this.upVote, this.downVote, this.favorite});
SomeObject copyWith({
int id,
String name,
int upVote,
int downVote,
int favorite,
}) {
return SomeObject(
id: id ?? this.id,
name: name ?? this.name,
upVote: upVote ?? this.upVote,
downVote: downVote ?? this.downVote,
favorite: favorite ?? this.favorite,
);
}
}

您可以按照自己喜欢的方式进行调整:upVoted复制upVote,其余部分保持不变,或者允许更改。

SomeObject upVoted() {
return SomeObject(
id: id, // no need for 'this' here
name: name,
upVote: upVote + 1,
downVote: downVote,
favorite: favorite,
);
}

将两者结合起来,你可能会产生无尽的变化:

SomeObject upVoted() => copyWith(upVote: upVote + 1);
SomeObject downVoted() => copyWith(downVote: downVote + 1);
SomeObject upVoteRetracted() => copyWith(upVote: upVote - 1);

这会让你开始怀疑为什么这个类是不可变的。似乎让它保持并变异状态比制作具有不同值的多个副本更有意义。

相关内容