Design Patterns: Observer, Interpreter, Memento Cheat Sheet

A Programming › Java cheat sheet in English by ppesq

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

25,841 views 4,744 downloads 2 Feb 2015 History
4.9 avg · 1 rating

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?

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?

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?

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.

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 3

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.