Philosophy behind the memento pattern

I have been reading up on memento pattern from various sources of the internet. Differing information from different sources has left me in confusion regarding why this pattern is actually needed.

The dofactory implementation says that the primary intention of this pattern is to restore the state of the system.

Wiki says that the primary intention is to be able to restore the changes on the system. This gives a different impact – saying that it is possible for a system to have memento implementation with no need to restore. And that ability of restore is a feature of this.

OODesign says that

It is sometimes necessary to capture the internal state of an object
at some point and have the ability to restore the object to that state
later in time. Such a case is useful in case of error or failure.

So, my question is why exactly do we use this one? Is it to save previous states – or to promote encapsulation between the Caretaker and the Memento? Why is this type of encapsulation so important?

Edit: For those visiting, check out this Implementation!

Edit: I am working in implementing a memento solution to my problem. I will post another question regarding that and link that question to this one. Thanks all for responding with valuable suggestions!

Edit 3: Here is the link to my sample implementation

0

A similar pattern, Memo, or Memoization, stores state as well, but it is often used as a program-speed optimization. If a time-consuming operation has a limited number of common inputs and outputs, the most common ones (or all of them) can be stored in a hashtable. When called again with the same inputs, it first checks the hashtable and if it finds them, it returns the previous output without recalculating it.

I imagine a Memento pattern could be used for performance as well – instead of doing all the reverse-calculations for a reverse state change, just restore from the previous state. Some functions are one-way, so there is no undo unless you store the previous state.

You could use a Memento pattern to memoize a periodic, or symmetric function like sine. Calculate all the values from 0-180 degrees, then go backwards through them to get the negative values from 180-360. Better yet, calculate values from 0-90, then go backwards through those values to get 90-180, forwards for 180-270, and backwards from 270-360.

Ctrl-Z in Word, or the undo function of any software is likely implemented by using the memento pattern, or in some cases, possibly by a reverse of the function that made each change. In the second case, the history of which functions were called would be mementos, so I suppose the memento pattern is always used for undo.

2

By itself, the Memento Pattern is just used to capture and save states. The encapsulation exists only to protect the states from the rest of the system – once a state is captured, it needs to be controlled carefully. For example, it doesn’t make sense to be able to change a previous state once it’s been saved (that would be changing history) and it might not make sense to simply return to a previous state of one object without affecting others (to prevent the system from entering an entirely invalid state).

The most common use of a Memento that I’ve seen is to support undo functionality. It is also related to storing redo functionality by moving back forward in time.

5

All these definitions steer you in the same direction; they are all saying the point is to be able to restore something to its previous state. That something can be the entire system, or just a single object.

This pattern is useful if a record changes state over time, but you have the business requirement to be able to restore it to any previous state at any time. Or alternatively, if you need to be able to view the record as it was at any previous point time. These types of requirements are very common in many types of systems.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật