Artificery

Menu

  • Home
  • Archives
  • Tags
  • About
  • Warrens
  • RSS
May 15, 2017

Memento Pattern

Intent

Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.

So want to be able to return to a previous state.

In clojure

Clojure's primary database Datomic is append only. This means you never lose state a previous state.

Critical insights come from knowing the full story of your data, not just the most recent state. Datomic stores a record of immutable facts, which gives your applications strong consistency combined with horizontal read scalability, plus built-in caching. Since facts are never update-in-place and all data is retained by default, you get built-in auditing and the ability to query history. All of this with fully ACID-compliant transactions.

The reality is that the GOF book talks about returning to a previous state "in memory" but has little to say about persistent storage. In clojure, or in any language, if you want to save a previous state i suggest a simple log. Possible a sufficient version of this is to use a vector:

(def drews-life (atom []))
(swap! drews-life conj "born")
(swap! drews-life conj "died")

get the current state

(last @drews-life)

summery

Maintaining real business logic state is something that really belongs in the database. Throwing away data is a business decision. Datomic errors on the side that more data is better sense storage is cheap. (though of course you can clean up unused data).

If you need to keep a history of something in memory, then a vector along side clojures stateful abstractions will probably do.


Tags: Software Clojure Behavrioral Design Pattern Java

« Interpreter Pattern Interator Pattern »

Copyright © 2024 Drew Verlee

Powered by Cryogen | Free Website Template by Download Website Templates