正确实施 Memento
在纪念品设计模式中,将 Caretaker 作为 Originator 的聚合体是错误的吗?
With the memento design pattern, is it wrong to have the Caretaker as an aggregate of the Originator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不必要。 Memento 背后的主要目的是可以以统一、透明的方式处理不同的 Originator(获取 Memento,稍后将其传回以恢复状态)。
聚合关系并不一定能阻止这种情况。 Caretaker 的不同实例可能是不同 Originator 的聚合(尽管 Caretaker 的一个实例仅处理 Originator 的一个实例),并且仍然可以获得透明状态恢复的好处。
Not necessarily. The main purpose behind Memento is that it is possible to deal with different Originators in uniform, transparent way (get Memento, pass it back later to restore state).
The aggregate relation doesn't necessarily prevent that. Different instances of Caretaker might be aggregates of different Originators (although one instance of Caretaker deals with only one instance of Originator) and still get the benefits of transparent state restoration.
不,只要适合您的情况就可以。 不难理解看守者的状态并不属于纪念品中保存的内容。
No, that's fine if it's what works out as appropriate for your situation. It's not a big leap to understand that the Caretaker's state isn't part of what's preserved in the Memento.