Design pattern of the day – Memento

Definition and use case from (https://dzone.com/refcardz/design-patterns)

Memento is a Behavioral Pattern( Used to manage algorithms, relationships, and responsibilities between objects ) .

Purpose
Allows for capturing and externalizing an object’s internal state so that it can be restored later, all without violating encapsulation.

Use When
• The internal state of an object must be saved and restored at a later time.
• Internal state cannot be exposed by interfaces without exposing implementation.
• Encapsulation boundaries must be preserved.
Example
Undo functionality can nicely be implemented using the memento pattern. By serializing and deserializing the state of an object before the change occurs we can preserve a snapshot of it that can later be restored should the user choose to undo the operation

How we are using memento in node-data

on every transaction api we provide an way for user to undo changes using entity manager

export interface EntityActionParam {
    inputEntity?: any;
    oldPersistentEntity?: any;
    newPersistentEntity?: any
}

using the manager you can always receive the input entity any where in the object execution cycle , allowing a developer to instantly rollback the transaction .

design_node_data

in Node-Data we have used memento in many places, If you want to know more about node-data check out the our github.
 
https://github.com/ratneshsinghparihar/Node-Data

Or visit our main page

https://nodedataio.azurewebsites.net/

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s