DTO 模式与 Memento 模式
DTO 模式(作者:Fowler)和 Memento 模式(由 GoF)在动机和实施方面?可以是同一个班级吗?如果是,我该如何命名它们(xxxDTO 或 xxxMemento)?它们在实施上有什么主要区别吗?它们在 MVP 架构中的位置是什么?
谢谢。
What are the differences between DTO pattern(by Fowler) and Memento pattern(by GoF) in motivation and implementation aspect? Can it be the same classes? If yes, how can I name them (xxxDTO or xxxMemento)? Do they have any principal difference in implementation? Where are their place in MVP architecture?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们有不同的用途。 DTO 是一种设计模式,用于在软件应用程序的层和/或多层之间传输对象。另一方面,备忘录是另一种设计模式,它允许对象通过外部化其状态来提供撤消功能,如果需要,可以在以后恢复该状态。类的 DTO 类和 Memento 类可能看起来相似,普通的旧 {Insert Technology Here} 对象 - PO?O 或看起来完全相同。然而,它们需要保持分离,因为它们将满足不同的需求,因此发展也不同。例如,有一天您可能需要在 DTO 类中包含一个属性,但同一属性对于对象的状态管理并不重要,因此不需要添加到 memento 类中。
另一个方面是,DTO 通常在您的班级之外进行,而您的班级对 DTO 并没有应有的了解。事实上,有一些框架可以为您处理数据映射。另一方面,对于 memento 模式,您的类需要提供一个 api,就像您在 uml 图中看到的那样,例如 CreateMemento、RestoreFromMemento 等。
They serve different purposes. DTO is a design pattern used to transfer objects between layers and/or tiers of a software application. Memento on the other hand is another design pattern that allows an object to provide an undo capability by externalizing its state which can later be restored if need be. A DTO class and a Memento class for a class may look similar, plain old {Insert Technology Here} object - PO?O or look exactly the same. However, they need to be kept separate because they will serve different needs and therefore evolve differently. For example, you may one day need to include a property in your DTO class but the same property is not important for the object's state management and therefore does not need to be added to the memento class.
Another aspect is that DTO usually takes place outside of your classes and your classes has no idea as they should about DTO. In fact, there are frameworks out there that take care of data mapping for you. For memento pattern on the other hand, your classes will need to provide an api like you see in the uml diagrams such as CreateMemento, RestoreFromMemento etc.
这两者经常用于非常不同的事情 - 你在哪里感到困惑? DTO 涉及数据传输(运营商类别),而 memento 涉及跟踪更改并允许您回滚这些更改。除此之外,我不确定是什么让你对你的问题感到困惑。
The two are often used for very different things - where are you confused here? DTO is about data transmission (carrier classes), while memento is about keeping track of changes and allowing you to roll those changes back. I am not sure, beyond that, what is confusing you from your question.