"Design Mode Design Pattern" Reading Notes

xiaoxiao2021-03-06  16

"Design Mode Design Pattern" Reading Notes

MEMENTO mode

structure

purpose

In order to restore an object to the previous state, it is not desirable to disclose the internal state so that the encapsulation is destroyed.

advantage

1. By saving the internal state of the Originator to MEMENTO instead of directing the internal state, it maintains the encapsulation of OrigInator. Because MEMENTO has two interfaces, the width interface will only be used by Originator, so that others cannot access these internal states. Caretaker can only see the Narrow interface of MEMENTO - it can only pass MEMENTO to other objects (ORIGINATOR). In C , this can be designed to a private function by designing Originator as a MEMENTO's Friend Class.

2. By saving the internal state of the Originator to the outside MEMENTO instead of Originator yourself to save, it is easy to understand and easy to understand.

Disadvantage

If you need to copy a lot of data from the Originator to Memento, it may make great costs.

If MEMENTO's creation and transmission to Originator is a foreseeable process, the information saved in MEMENTO can be just those that have changed data (INCREMENTAL CHANGE), not necessarily the entire ORIGINATOR internal information. copy. For example, History List in Command mode, you can save the Incremental Change for each Commando generated by MEMANTO.

Reference

"Design Pattern" Gang-Of-4 1997

转载请注明原文地址:https://www.9cbs.com/read-41541.html

New Post(0)