Show Menu
Cheatography

Design Patterns: Observer, Interpreter, Memento Cheat Sheet by

Brief overview of some, common and not so common behavioural design patterns.

OBSERVER

one-to­-many dependency between subject and observers, so that when subject changes, the observers are notified and updated.
a way of notifying change to a number of classes

Questions

What is it?
many objects need to be notified of changes in the state of one object
Where have I seen it before?
RSS feeds or any pub/sub system you might have used/coded

Ok, this is cool. What do I need to implement it?

1. A Subject Abstract Class and an Observer Abstract Class
2. Concrete subject and observer class that implement above pattern.
The concrete subject registers its observers

Example

A referee (concrete subject) notifies all the players and commen­tators (concrete observers) about changes in the state of a Soccer match. Each player must be notifi­able.
 

MEMENTO

provides the ability to restore an object to its previous state
a memento is like a magic cookie that encaps­ulates a checkpoint capability

Questions

What problem does it solve?
you want to save the state of an object so that you can restore it later.
Where have I seen it before?
Git or any version control system for that matter. A memento makes rollbacks possible.

Ok, this is cool. What do I need to implement it?

1. An originator class (class that has a state that needs to be rememb­ered)
2. A caretaker class (class that wants to modify the state of the origin­ator)
3. A memento class that holds originator inform­ation that can't be modified by any other class. It is merely a container.

Example

A programmer (caret­aker) asks for a copy (memento) of the code (origi­nator) he/she is modifying. Later he/she decides he doesn't like the new state of the code so he restores it with the copy it still has.
 

INTERP­RETER

Represent the grammar of a language with a hierar­chical object­-or­iented design.
The language is usually a domain specific language.

Questions

What problem does it solve?
A language must be parsed and interp­reted.
Where have I seen it before?
Parsers

Ok, this is cool. What do I need to implement it?

1. A Context class that contains the input.
2. An Abstra­ctE­xpr­ession class, a composite object of terminals and non-te­rmi­nals.
3. The client passes the context to the abstract expres­sion, which calls the interp­ret() function on its children.

Example

A roman numeral (context) is converted into decimal notation by the parser (Abstract Expres­sion).
Derivation: LIV => 50 + IV => 50 + (-1 + 5) => 50 + 4 => 54

Advantages

Observer: minimal coupling; easy addition and removal of observers
Memento: it is an encaps­ulated copy so it avoids exposing its info; the storage burden is on the caretaker, not on originator
Interp­reter: easy to change­/ex­ten­d/i­mpl­eme­nt/­eva­luate a language

Disadv­antages

Observer: Possible memory leak; Objects might need to work hard to deduce what changed in the subject.
Memento: Copy operation to a memento can be costly for the origin­ator; Caretaker might have large storage costs.
Interp­reter: Complex grammars are hard to maintain and debug.
                           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Cypressio Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet

          More Cheat Sheets by ppesq