Show Menu

Design Patterns: Observer, Interpreter, Memento Cheat Sheet by

Brief overview of some, common and not so common behavioural design patterns.
design     java     software     observer     patterns     designpattern     memento     interpreter

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 refe­ree (concrete subject) notifies all the play­ers and comm­ent­ators (concrete observ­ers) 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 prog­ram­mer (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).
Deriv­ation: LIV => 50 + IV => 50 + (-1 + 5) => 50 + 4 => 54

Advantages

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

Disadv­antages

Obse­rver: Possible memory leak; Objects might need to work hard to deduce what changed in the subject.
Meme­nto: Copy operation to a memento can be costly for the origin­ator; Caretaker might have large storage costs.
Inte­rpr­eter: Complex grammars are hard to maintain and debug.

Download the Design Patterns: Observer, Interpreter, Memento Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/ppesq_design-patterns-observer-interpreter-memento.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

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

          JavaScript Cheat Sheet
          Selenium WebDriver Cheat Sheet Cheat Sheet
          System Design Cheat Sheet

          More Cheat Sheets by ppesq