DTO pattern vs Memento pattern
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 xxxMemen开发者_高级运维to)? Do they have any principal difference in implementation? Where are their place in MVP architecture?
Thanks.
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.
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.
精彩评论